Na drugom primeru je pokusaj da se objasni lock statement.Jasno mi je da je lock statement zaduzen za zakljucavanje dela koda , da bih obezbedili da samo jedna nit "dira" dati resurs u jednom trenutku vremena (to jest dok je u lock bloku). To je jasno,ali mi sintaksa nije jasna. Zasto pisati (kao u prvom primeru) lock za neki objekat koji nigde nema referencu, osim u lock statementu??? Cemu to ? Radi kozmetike? Jasnije bi bilo da sintaksa radi nesto tipa : lock(balance){ //nesto } (posto u sustini necemo da nam balance promenljivu neko dira, gledajuci drugi primer), ali lock radi samo na referentnim tipovima.... pa to ne moze da radi. Nista tu ne razumem, ali apsolutno nista. Koji je mozak smisljao takvu sintaksu....
Hteo bih da zamolim nekog da mi objasni ove nedoumice.
Hvala vam unapred.
Hteo bih da zamolim nekog da mi objasni ove nedoumice.
Hvala vam unapred.
Code:
lock (thisLock)
{
//pa nesto
}
lock (thisLock)
{
//pa nesto
}
Code:
// using System.Threading;
class Account
{
private Object thisLock = new Object();
int balance;
Random r = new Random();
public Account(int initial)
{
balance = initial;
}
int Withdraw(int amount)
{
// This condition will never be true unless the lock statement
// is commented out:
if (balance < 0)
{
throw new Exception("Negative Balance");
}
// Comment out the next line to see the effect of leaving out
// the lock keyword:
lock (thisLock)
{
if (balance >= amount)
{
Console.WriteLine("Balance before Withdrawal : " + balance);
Console.WriteLine("Amount to Withdraw : -" + amount);
balance = balance - amount;
Console.WriteLine("Balance after Withdrawal : " + balance);
return amount;
}
else
{
return 0; // transaction rejected
}
}
}
public void DoTransactions()
{
for (int i = 0; i < 100; i++)
{
Withdraw(r.Next(1, 100));
}
}
}
class Test
{
static void Main()
{
Thread[] threads = new Thread[10];
Account acc = new Account(1000);
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(acc.DoTransactions));
threads[i] = t;
}
for (int i = 0; i < 10; i++)
{
threads[i].Start();
}
}
}
// using System.Threading;
class Account
{
private Object thisLock = new Object();
int balance;
Random r = new Random();
public Account(int initial)
{
balance = initial;
}
int Withdraw(int amount)
{
// This condition will never be true unless the lock statement
// is commented out:
if (balance < 0)
{
throw new Exception("Negative Balance");
}
// Comment out the next line to see the effect of leaving out
// the lock keyword:
lock (thisLock)
{
if (balance >= amount)
{
Console.WriteLine("Balance before Withdrawal : " + balance);
Console.WriteLine("Amount to Withdraw : -" + amount);
balance = balance - amount;
Console.WriteLine("Balance after Withdrawal : " + balance);
return amount;
}
else
{
return 0; // transaction rejected
}
}
}
public void DoTransactions()
{
for (int i = 0; i < 100; i++)
{
Withdraw(r.Next(1, 100));
}
}
}
class Test
{
static void Main()
{
Thread[] threads = new Thread[10];
Account acc = new Account(1000);
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(acc.DoTransactions));
threads[i] = t;
}
for (int i = 0; i < 10; i++)
{
threads[i].Start();
}
}
}