Using Metux in Java multithreaded synchronization design

Source: Internet
Author: User
Tags mutex thread

Mutexes are mutexes that are widely used in multithreaded programming. In this paper, we discuss the implementation of the mutex of Doug Lea's concurrent Toolkit in a wide-flow case. In Doug Lea's concurrent toolkit, the mutex implements the sync interface, which is the public interface for all locks (lock), Gate (gate), and condition variables (condition) in the Concurrent toolkit. The realization classes of sync mainly include: Mutex, Semaphore and its subclasses, Latch, Countdown, Reentrantlock, etc. This also embodies the idea of abstract programming, which allows us to choose the different implementations of sync without changing the code or changing a small amount of code. The following is the definition of the Sync interface:

public interface Sync
{
  public void acquire() throws InterruptedException;
  //获取许可
  public boolean attempt(long msecs) throws InterruptedException;
  //尝试获取许可
  public void release();
  //释放许可
}

By using sync, you can replace the Java synchronized keyword and provide more flexible synchronization control. Of course, not to say concurrent toolkit is and Java synchronized independent technology, in fact, concurrent toolkit is also built on the basis of synchronized, from the following to the mutex source analysis can see this. The Synchronized keyword is valid only within a method or within a block of code, but using sync can span the method and even pass through the objects to synchronize across the object. This is where the sync and concurrent kits are more powerful than the direct use of synchronized.

Note that acquire () and attempt () in sync throw interruptedexception, so when you use sync and its subclasses, calling these methods must capture interruptedexception. The release () method does not throw interruptedexception, because wait () may be invoked in the acquire () and attempt () methods, waiting for other threads to free the lock. and release () on the implementation of the simplification, directly released the lock, whether or not the real hold. So you can call release () on a thread that doesn't have a acquire (), and that's not going to be a problem. And since release () does not throw interruptedexception, we can call release () in a catch or finally clause to ensure that the acquired lock is released correctly. Like what:

class X
{
  Sync gate; // ...
  public void m()
  {
   try
   {
    gate.acquire();
    // block until condition holds
    try
    {
     // ... method body
    }
    finally { gate.release(); }
   }
   catch (InterruptedException ex) { // ... evasive action }
  }
}

A mutex is a mutex that is not reentrant. Mutexes are widely used in synchronous environments that require before/after types that span methods. Here is the implementation of the mutex in Doug Lea's concurrent toolkit.

public class Mutex implements Sync
{
/** the lock status **/
protected Boolean inuse_ = false;
public void Acquire () throws Interruptedexception
{
if (thread.interrupted ()) throw new Interruptedexception ();//(1)
Synchronized (This)
{
Try
{
while (Inuse_) wait ();
Inuse_ = true;
}
catch (Interruptedexception ex)
{
(2)
Notify ();
Throw ex;
}
}
}
Public synchronized void release ()
{
Inuse_ = false;
Notify ();
}
Public boolean attempt (long msecs) throws Interruptedexception
{
if (thread.interrupted ()) throw new Interruptedexception ();
Synchronized (This)
{
if (!inuse_)
{
Inuse_ = true;
return true;
}
else if (msecs <= 0)
return false;
Else
{
Long waittime = msecs;
Long start = System.currenttimemillis ();
Try
{
for (;;)
{
Wait (waittime);
if (!inuse_)
{
Inuse_ = true;
return true;
}
Else
{
waittime = msecs-(System.currenttimemillis ()-start);
if (waittime <= 0)//(3)
return false;
}
}
}
catch (Interruptedexception ex)
{
Notify ();
Throw ex;
}
}
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.