We all know that before jdk1.5, when Java needs to implement business concurrency, programmers usually need to implement Code independently. When designing high quality Java multi-thread concurrent programs, to prevent the appearance of such phenomena as death, such as using wait (), Y (), and synchronized before Java, every time you need to consider performance, deadlock, fairness, resource management, and how to avoid the harm caused by thread security, some complicated security policies are often used, increases the development burden of programmers. fortunately, after the emergence of jdk1.5, Sun finally launched Java for our poor programmers. util. concurrent toolkit to simplify concurrency. With this, developers can effectively reduce race conditions and deadlock threads. The concurrent package solves these problems and provides us with a more practical concurrent program model.
Main interfaces and classes in Java. util. Concurrent:
Executor: executor of a specific runnable task.
Executorservice: A thread pool manager, whose implementation classes include common thread pools and regular scheduling thread pools scheduledexecutorservice.
Runnable and callable are submitted to the pool for scheduling.
Future: it is an interface that interacts with runnable and callable. For example, a thread obtains the returned results after execution ends, and a cancel termination thread is also provided.
Blockingqueue: blocking the queue.
Below is a simple example program:
Futureproxy. JavaView copies to clipboard Printing
- Package org. Test. Concurrent;
- /***//**
- * <P> title: loonframework </P>
- * <P> Description: Processing in future mode </P>
- * <P> copyright: Copyright (c) 2007 </P>
- * <P> company: loonframework </P>
- * @ Author chenpeng
- * @ Email: ceponline@yahoo.com.cn
- * @ Version 0.1
- */
- Import java. Lang. Reflect. invocationhandler;
- Import java. Lang. Reflect. method;
- Import java. Lang. Reflect. proxy;
- Import java. util. Concurrent. callable;
- Import java. util. Concurrent. executorservice;
- Import java. util. Concurrent. executors;
- Import java. util. Concurrent. Future;
- Import java. util. Concurrent. threadfactory;
- Public abstract class futureproxy <t> {
- Private final class callableimpl implements callable <t> {
- Public t call () throws exception {
- Return futureproxy. This. createinstance ();
- }
- }
- Private Static class invocationhandlerimpl <t> implements invocationhandler {
- Private future <t> future;
- Private volatile t instance;
- Invocationhandlerimpl (Future <t> future ){
- This. Future = future;
- }
- Public object invoke (Object proxy, method, object [] ARGs)
- Throws throwable {
- Synchronized (this ){
- If (this. Future. isdone ()){
- This. instance = This. Future. Get ();
- } Else {
- While (! This. Future. isdone ()){
- Try {
- This. instance = This. Future. Get ();
- } Catch (interruptedexception e ){
- Thread. currentthread (). Interrupt ();
- }
- }
- }
- Return method. Invoke (this. instance, argS );
- }
- }
- }
- /***//**
- * Implement the java. util. Concurrent. threadfactory Interface
- * @ Author chenpeng
- *
- */
- Private Static final class threadfactoryimpl implements threadfactory {
- Public thread newthread (runnable R ){
- Thread thread = new thread (R );
- Thread. setdaemon (true );
- Return thread;
- }
- }
- Private Static executorservice service = executors. newcachedthreadpool (New threadfactoryimpl ());
- Protected abstract t createinstance ();
- Protected abstract class <? Extends T> getinterface ();
- /***//**
- * Returns the proxy instance.
- * @ Return
- */
- @ Suppresswarnings ("unchecked ")
- Public final t getproxyinstance (){
- Class <? Extends T> interfaceclass = This. getinterface ();
- If (interfaceclass = NULL |! Interfaceclass. isinterface ()){
- Throw new illegalstateexception ();
- }
- Callable <t> task = new callableimpl ();
- Future <t> future = futureproxy. Service. Submit (task );
- Return (t) proxy. newproxyinstance (interfaceclass. getclassloader (),
- New Class <?> [] {Interfaceclass}, new invocationhandlerimpl (Future ));
- }
- }
Package Org. test. concurrent;/*** // *** <p> title: loonframework </P> * <p> description: processing in future mode </P> * <p> copyright: Copyright (c) 2007 </P> * <p> company: loonframework </P> * @ author chenpeng * @ Email: ceponline@yahoo.com.cn * @ version 0.1 */import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; import Java. lang. reflect. proxy; import Java. util. concurrent. callable; import Java. util. conc Urrent. executorservice; import Java. util. concurrent. executors; import Java. util. concurrent. future; import Java. util. concurrent. threadfactory; public abstract class futureproxy <t> {private final class callableimpl implements callable <t> {public t call () throws exception {return futureproxy. this. createinstance () ;}} Private Static class invocationhandlerimpl <t> implements invocationhandler {private Future <t> future; private volatile t instance; invocationhandlerimpl (Future <t> future) {This. future = Future;} public object invoke (Object proxy, method, object [] ARGs) throws throwable {synchronized (this) {If (this. future. isdone () {This. instance = This. future. get ();} else {While (! This. future. isdone () {try {This. instance = This. future. get ();} catch (interruptedexception e) {thread. currentthread (). interrupt () ;}} return method. invoke (this. instance, argS) ;}}/ ***** // *** implement Java. util. concurrent. threadfactory interface * @ author chenpeng */Private Static final class threadfactoryimpl implements threadfactory {public thread newthread (runnable R) {thread = new thread (R ); Thread. setdaemon (true); Return thread ;}} Private Static executorservice service = executors. newcachedthreadpool (New threadfactoryimpl (); protected abstract t createinstance (); protected abstract class <? Extends T> getinterface ();/*** // *** return proxy instance * @ return */@ suppresswarnings ("unchecked") Public final t getproxyinstance () {class <? Extends T> interfaceclass = This. getinterface (); If (interfaceclass = NULL |! Interfaceclass. isinterface () {Throw new illegalstateexception ();} callable <t> task = new callableimpl (); Future <t> future = futureproxy. service. submit (task); Return (t) proxy. newproxyinstance (interfaceclass. getclassloader (), new class <?> [] {Interfaceclass}, new invocationhandlerimpl (Future ));}}
Test. JavaView copies to clipboard Printing
- Package org. Test. Concurrent;
- Import java. util. calendar;
- /***//**
- * <P> title: loonframework </P>
- * <P> Description: </P>
- * <P> copyright: Copyright (c) 2007 </P>
- * <P> company: loonframework </P>
- * @ Author chenpeng
- * @ Email: ceponline@yahoo.com.cn
- * @ Version 0.1
- */
- Interface datetest {
- String getdate ();
- }
- Class datetestimpl implements datetest {
- Private string _ date = NULL;
- Public datetestimpl (){
- Try {
- _ Date + = calendar. getinstance (). gettime ();
- // Set the latency of five seconds
- Thread. Sleep (5000 );
- } Catch (interruptedexception e ){
- }
- }
- Public String getdate (){
- Return "date" + _ date;
- }
- }
- Class datetestfactory extends futureproxy <datetest> {
- @ Override
- Protected datetest createinstance (){
- Return new datetestimpl ();
- }
- @ Override
- Protected class <? Extends datetest> getinterface (){
- Return datetest. Class;
- }
- }
- Public class test {
- Public static void main (string [] ARGs ){
- Datetestfactory factory = new datetestfactory ();
- Datetest [] DTS = new datetest [100];
- For (INT I = 0; I <DTS. length; I ++ ){
- DTS [I] = factory. getproxyinstance ();
- }
- // Execute Traversal
- For (datetest DT: DTS ){
- System. Out. println (Dt. getdate ());
- }
- }
- }