[J2SE 5.0 Topics] Concurrent Toolkit

xiaoxiao2021-03-06  19

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)

Similarly, a presentation code: Executor E = New xxxexecutor (); // Demonstration Executor Implementation class E.EXECUTE (New Runnable () {...}; E.EXECUTE () {...}) Specific this Runnable instance end what kind of thread environment is performed in any way, completely determined by the Executor's implementation class xxxexecutor, rather than writing to this code. For more detailed Java concurrency programming, reference to Java.util.Concurrent Packages and Doug LEA's homepage.

转载请注明原文地址:https://www.9cbs.com/read-39788.html

New Post(0)