Tässä koodi, jonka löysin alunperin muualta, mutta hieman muokkasin sitä:
public class TCriticalSection {
/** Creates a new instance of TCriticalSection */
public TCriticalSection() {
}
// ----------------------
/* The lock status */
protected boolean lckAvailable = true;
public void acquire () throws InterruptedException
{
if (Thread.interrupted())
throw new InterruptedException();
synchronized(this)
{
try
{
while (!lckAvailable ) wait();
lckAvailable = false;
}
catch (InterruptedException ex)
{
notifyAll();
throw ex;
}
}
}
public synchronized void release ()
{
lckAvailable = true;
notifyAll();
}
}
Sille, joka haluaa verrata alkuperäiseen, voit kurkata tänne:
http://www.tol.oulu.fi/~avesanen/Rinn_Ohjelm/Luennot/Poissulkevuus.html
public class TCriticalSection {
/** Creates a new instance of TCriticalSection */
public TCriticalSection() {
}
// ----------------------
/* The lock status */
protected boolean lckAvailable = true;
public void acquire () throws InterruptedException
{
if (Thread.interrupted())
throw new InterruptedException();
synchronized(this)
{
try
{
while (!lckAvailable ) wait();
lckAvailable = false;
}
catch (InterruptedException ex)
{
notifyAll();
throw ex;
}
}
}
public synchronized void release ()
{
lckAvailable = true;
notifyAll();
}
}
Sille, joka haluaa verrata alkuperäiseen, voit kurkata tänne:
http://www.tol.oulu.fi/~avesanen/Rinn_Ohjelm/Luennot/Poissulkevuus.html
Vastaukset
Vanhimmat
Ei vielä vastauksia
Aloittaja odottaa vastauksia. Kerro oma näkemyksesi ensimmäisenä.