Discussion on concurrent processing of java.util.concurrent package

Source: Internet
Author: User
As we all know, before JDK1.5, Java in the business concurrency, often need to have a programmer to complete the code implementation, and when the high quality Java multithreaded concurrent programming, to prevent the emergence of such phenomena, such as the use of Java before the Wait (), notify () and synchronized, and so on, often need to consider performance, deadlock, fairness, resource management and how to avoid the harm caused by thread security and many other factors, tend to adopt some more complex security policies, Increases the programmer's burden of development. Luckily, after JDK1.5, Sun god finally launched the Java.util.concurrent Toolkit for our poor little programmers to simplify concurrency. Developers use this to effectively reduce competitive conditions (race conditions) and deadlock threads. The concurrent package solves these problems well and provides us with a more practical model of concurrent programs.


The main interfaces and classes under Java.util.concurrent:

Executor: The performer of a specific runnable task.

Executorservice: A thread pool manager, its implementation classes have a variety of, such as the common thread pool, timed dispatch thread pool Scheduledexecutorservice, etc., we can put a

Runnable,callable commits to the pool for scheduling.

Future: is an interface that interacts with runnable,callable, such as the result of a thread being returned after execution, and the cancel termination thread is also provided.

Blockingqueue: Blocking queues.

Below I write a simple case program:

Futureproxy. java
Package org.test.concurrent;
/** */ /**
* <p>Title:LoonFramework</p>
* <p>description: Using future mode for processing </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 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

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.