From: http://my.oschina.net/dxf/blog/239
After jdk1.5, the Java. util. Concurrent package is provided, which can implement the thread pool. You can regard the thread as a common object, which is responsible for scheduling and execution.
Including two types of thread pools
Fixed Thread Pool
Variable Thread Pool
Delay Thread Pool
Fixed Thread Pool
Public static executorservice newfixedthreadpool (INT nthreads) multi-task
Public static executorservice newsinglethreadpool () single task
Executorservice Method
Excute (runnablecommand );
Shutdown ();
Usage:
Mytask MT1 = new mytask ("T1"); // a thread object instance
Executorservice threadpool = executors. newfixedthreadpool (2 );
Threadpool. excute (MT1 );
Variable Thread Pool
Public static executorservice newcachedthreadpool ()
Delay thread pool for scheduled tasks
Public static scheduledexecutorservice newscheduledthreadpool (INT poolsize)
Instead of using the excute method, it uses the schedule method.
Public schedualedfuture schedule (runnable command, long delay, timeunit Unit );
Thread Pool with return value
The callable interface works with the future interface, which is used to accept the returned value.
The callable interface works the same as the runnable interface, but it is a call interface method. This method also has a return value.
Class mycallableimpl implements callable
{
Public object call ()
{
}
}
Use
Executorservice threadpool = executors. newsinglethreadexector ();
Future F = threadpool. Submit (New mycallableimpl ();
Resource Blocking
We know that the syncnized method can block resources of a piece of code. In fact, there are many other methods. Here we will summarize them.
1: Synchronized
2: Volatile
3: implement the reentrantlock class of the lock interface. The methods include lock (), unlock (), and trylock (). Be sure to try ...... Finally to prevent deadlocks
4: The readwritelock interface implements the reentrantreadwritelock class. The methods are readlock and writelock. The method is generally the same as the lock interface, but its efficiency is high. Also avoid deadlocks
5: semaphores semaphore class. semaphores are used to allocate resources, but they also have the lock feature. For example, the connection pool can be used without the explosion of the connection pool, main Methods: acquire (), acquire (int n), tryacquire (), getqueuelength (), release ()
6: Atomic objects. After jdk15, some basic types can be defined as atomic objects for single-thread operations to simplify operations. Java. util. Concurrent. Atomic, which serves basically the same variable volatile
7: Barrier, cyclicbarrier class, to synchronize threads to the same point
Queue and stack
Java. util. Queue Interface
Public Boolean offer (object); add
Public object poll ();
Peek (); out, but not deleted
Remove (); same as poll
Element (); same as peek
Add (); same as offer
Common implementations: Java. util. shortlist and Java. util. priorityqueue
Blockingqueue Interface
Java. util. Concurrent. blockingqueue
Put (object );
Take (); Out
Blockingdeque Interface
It is a blocking stack interface.
Putfirst (Object O );
Takefirst ();
Putlast ();
Takelast ();