Thread concurrency library summary:
1. Data sharing within the thread range: Simply put, two classes (external class or internal class) are called in a thread) the data is unique within the scope of the Thread or during running (you can put the data in a generic map <Thread, Integer> in advance ). This method is widely used in database applications, and transactions are committed when transactions are started.
ThreadLocal is a class that provides this function in Java, but only one data can be put but can be a set and an object.
2. How do two internal classes in an external class share data?
All operations on the same member of the external class: for example, A internal class has B and C External class B and C access the same member of A (such as an object)
AtomicInteger implements thread synchronization security for Integer data, and other basic data types are also available.
AtomicIntegerFieldUpdater can perform atomic update on the specified int field of the specified class, similar to reflection.
When the thread pool comes up, it creates multiple thread objects and waits for the service. If a large number of services are required and the service threads are insufficient, the service will be blocked, the jdk comes with the thread pool Executors class (fixed number of threads, cache threads, and a single thread (if the thread ends abnormally, it will be replaced by a thread to solve the problem of thread death )) you can also create thread pools similar to the timer function. For details, see the Executors class.
The content returned after the thread is executed is as follows:
ExecutorService threadPool = Executors. newSingleThreadExecutor ();
Future <String> future =
ThreadPool. submit (
New Callable <String> (){
Public String call () throws Exception {
Thread. sleep (2000 );
Return "hello ";
};
}
);
System. out. println ("waiting for results ");
Try {
System. out. println ("get Result:" + future. get ());
You can also get the result within the specified time, such as future. get (1, TimeUnit. seconds)
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
CompletionService class one-time submission of a set of Callable tasks that are completed first.
For example, the following program submits 10 tasks and then gets the returned values for the for Loop respectively.
ExecutorService threadPool2 = Executors. newFixedThreadPool (10 );
CompletionService <Integer> completionService = new ExecutorCompletionService <Integer> (threadPool2 );
For (int I = 1; I <= 10; I ++ ){
Final int seq = I;
CompletionService. submit (new Callable <Integer> (){
@ Override
Public Integer call () throws Exception {
Thread. sleep (new Random (). nextInt (5000 ));
Return seq;
}
});
}
For (int I = 0; I <10; I ++ ){
Try {
System. out. println (
CompletionService. take (). get ());
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (ExecutionException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
3. lock in the concurrent library in java)
Lock is similar to synchronized, but it is more object-oriented than synchronized.
The code snippets executed by two threads must use the same lock object to implement synchronization and mutual exclusion. The lock is in the internal method of the class that represents the resource to be operated, rather than in the thread code.
Read/write locks: Multiple read locks are not mutually exclusive. read/write locks are mutually exclusive. (The api example is more classic)
For example: ReadWriteLock rwl = new ReentrantReadWriteLock ();
User user = seeion. load (id, user. calss) in hibeat)
User user = seeion. get (id, user. calss)
The second difference is to retrieve the User object directly from the database. If the User object is not obtained, null is returned. The second will return the real User object proxy concurrency regardless of whether the User object exists in the database.
Returned proxy code
The Condition function is similar to the wait and policy functions in traditional thread technology. However, it can selectively notify any Condition and must be used together with lock.
Semaphore Traffic Signal class, which can maintain the number of threads currently accessed and provides a synchronization mechanism (provided that when only one traffic signal is available and one thread acquires the lock, another thread can release the lock ), the basic principle is similar to the signal action principle.
CyclicBarrier thread wait class: The basic function is to go back when 10 threads reach the same point. The first thread waits for other threads that have not arrived, for example, if there are 10 conventions to meet at the railway station and then take a bus to travel together, everyone is a thread, waiting for a person to arrive and then take a bus together.
CountDownLatch is equivalent to notifying multiple threads or one thread when the timer reaches 0.
Exchager is used to exchange data between two threads. Each thread wants to exchange data with each other after completing a certain transaction, the first thread to fetch data will wait until the second thread receives the data to exchange data with each other.
To put it simply, data is exchanged when both threads reach the same place. For example, sellers selling toxic powder are waiting for buyers (or vice versa) when the buyer arrives, the transaction is made and then quickly leaves.
BlockingQueue is a bounded blocking queue. SyschronousQueue stores a blocking queue.
Set synchronization Security ConcurrentMap and Other interfaces