Mutex是一個非重入的互斥鎖
public class Mutex implements Sync
{
/** The lock status **/
protected boolean inuse_ = false;
public void acquire() throws InterruptedException
{
if (Thread
synchronized(this)
{
try
{
while (inuse_) wait();
inuse_ = true;
}
catch (InterruptedException ex)
{
//(
notify();
throw ex;
}
}
}
public synchronized void release()
{
inuse_ = false;
notify();
}
public boolean attempt(long msecs) throws InterruptedException
{
if (Thread
synchronized(this)
{
if (!inuse_)
{
inuse_ = true;
return true;
}
else if (msecs <=
return false;
else
{
long waitTime = msecs;
long start = System
try
{
for (;;)
{
wait(waitTime);
if (!inuse_)
{
inuse_ = true;
return true;
}
else
{
waitTime = msecs
if (waitTime <=
return false;
}
}
}
catch (InterruptedException ex)
{
notify();
throw ex;
}
}
}
}
}
[
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27680.html