2016-1- 2
Thread Communication
Traditional thread Communication
The Object class provides wait (),notify () , and Notifyall three methods
Application:synchronized modified synchronous method or synchronized method
Wait (): causes the current thread to wait until another thread calls the synchronization Monitor's notify () or notifyall method to wake the thread, calling wait method, this thread releases the lock on the synchronization monitor
notify (): wakes up a single thread waiting on this synchronization monitor. If more than one thread is waiting, one is randomly awakened
notifyall (): wakes up all the threads waiting on this sync monitor
Using Condition to control thread communication
For scenarios that use the lock object to synchronize, theCondition instance is bound to a lock object
Lock replaces synchronous or synchronous code block,Condition replaces the function of synchronization monitor
await (), signal (), Signalall ()
Private final Lock lock=new Reentrantlock ();
Private final Condition cond=lock.newcondition ();
Cond.await ()
Cond.signalall ()
-
blockingqueue Control thread communication
-
add (e E), off (e), put (e)
-
remove (), poll (), take ()
-
element (), Peek ()
Arrayblockinqueue, Linkedblockingqueue, Priorityblockingqueue, Synchronousqueue, Delayqueue
Blockingqueue<stirng> bq=new arrayblockingqueue<> (2);
Bg.put ("Java")
Thread Groups (Threadgroup) and unhandled exceptions
Programs can control threads directly on a group-by-unit
Cannot change the owning thread group while the thread is running
Threadgroup:activecount (), interrupt (), Isdaemon (), Setdaemon (), setmaxpriority ()
A useful method is also defined in threadgroup :void uncaughtexception (thread t,throwable e), which can handle exceptions thrown by any thread within a thread group
When catching an exception with catch , the exception is not propagated to the previous caller, but when the exception is handled with the exception handler, the exception is still propagated to the previous caller
Thread pool
When you need to create lots of short-lived threads in your program, you should consider using a thread pool. Thread pool creates a large number of idle threads at system startup
Thread pool implemented by Java5
Returns a Executorservice object that represents a thread pool:newcachedthreadpool () newfixedthreadpool (int nthreads), Newsinglethreadexecutor ()
New Scheduledthreadpool (int corepoolsize), new Singlethreadscheduledexecutor ()
To use the thread pool to perform the threading task:
Call the static factory method of the executors class to create a Executorservice object that represents a thread pool;
Create an instance of the Runnable implementation class or callable implementation class to perform the task as a thread;
Invoke the submit () method of the Executorservice object to submit the Runnable instance or callable instance;
The task finishes and finally closes the thread pool shutdown ().
Java7 The new forkjoinpool
Take advantage of the performance benefits of multi- CPU, multi-core CPUs
Split a task into multiple "small tasks" parallel computations and combine the results of multiple small tasks into a total calculation result
Forkjoinpool is an implementation class of Executorservice , so it is a special thread pool
Created aForkjoinpoolinstance, you can call theForkjoinpoolOfSubmit (forkjointask Task)OrInvoke (forkjointask Task)method to perform the specified task. whichForkjointaskRepresents a task that can be merged in parallel.Forkjointaskis an abstract class, and it has two abstract subclasses: RecursiveactionAndRecursivetask。 whichRecursivetaskRepresents a task with a return value, while therecursiveactionRepresents a task that has no return value.
Thread-related classes
Java reading notes (4)-Multithreading (two)