[J2SE 5.0 Topics] [4.2] Concurrent Toolkit
Doug Lea is a big manager of Java. I want a lot of people to have heard that CONCURRENT Programming In Javatmdesign Principles and patterns, almost a book that learns Java concurrent programming must see. This new concurrent kit introduced in JSR-166 in J2SE 5.0 is absolutely worthy of attention. The newly introduced java.util.concurrent package, as well as the implementation and API use of the API, you can write a book or even a book, I want to avoid light, pick two simple understanding, interested Friends can refer to some books and online information. I plan to take a look at the Semaphore class and the Executor interface. Semaphore is actually a pass administrator, in a design environment, every thread needs to get a pass from the administrator, then deal with the thing, after processing, then Use the pass to return to the administrator for other threads. For a relatively extreme situation: there is only one pass, this time there may be a thread to deal with that thing, which is similar to the implementation of Synchronized. Semaphore is actually more flexible than Wait () or Synchronized. Come a little actual code snippet: Private final semaphore s = new semaphore (1); // "Only one pass can be used" S.Acquireuninterruptibly (); // "Try to get a pass until" // "Holds a pass To complete one thing ... "S.Release (); //" Returning this pass "and Executor is a good abstract thread executation framework, which puts the specific thread processing from our specific code Separate, implement specific thread processing mechanisms in another place: such as single thread, or thread pool, etc. Executor only declares a method:
Void Execute (Runnable Command) is also a presentation code:
Executor E = New xxxexecutor (); // Imaginary Executor Implementation Class E.Execute (New Runnable () {...}; E.EXECUTE (NEW Runnable () {...});
The specific this RunNable instance is ultimately in what thread environment and what kind of manner is performed, completely determined by the implementation class XXExecutor of Executor, rather than writing to this code.
Concurrent programming for more detailed Java, reference
Java.util.concurrent package and
The homepage of Doug Lea.