Background notes:
Multithreading concurrency and management, is the focus of Java basic knowledge, this article according to "Java Core Technology Eighth edition" in the multi-threaded technology learning, the knowledge points are collated; here only a simple list of basic knowledge points, in order to achieve the knowledge point has a mesh-related effect, can play a sketchy role, In other points of knowledge fusion, there is a better list of effects.
Threading Concepts
1. Identify the difference between a process and a thread
2. Do not call the Run method of the thread or Runnable object, call the Run method directly, execute only the tasks in the same thread, and do not start a new thread. The Thread.Start method should be called. This method creates a new thread to execute the Run method.
Break Thread in
1. The method of requesting the terminating thread is the interrupt method
2. When calling the interrupt method in a thread blocking state, a interruptexception exception is generated.
3. There are 2 similar methods, interrupted and isinterrupted methods; Interrupted is a static method that detects whether the current thread is interrupted and clears the interrupt state of the thread; isinterrupted is an instance method. can be used to verify that the thread is terminated without changing the interrupt state of the thread.
Thread state
- New
- Runnable Operational Status
- Blocked is blocked.
- Waiting wait
- Timed Waiting Timing Wait
- Terminated is terminated.
Key notes:
Thread's Stop,suspend,resume method is obsolete.
The Thread.Join method waits for the specified thread to terminate. Note: If there are 3 threads, which are executed sequentially, the emphasis is on sequential execution, and the main observation is that this method is used.
Thread Properties
Attributes include: Thread priority, daemon thread, thread group, and processor handling uncaught exception
1. Priority level
You can set the minimum to maximum: 1 to 10,norm_priority is defined as 5
2. Daemon Threads
function is to serve other threads
Synchronous
Common examples are multiple threads accessing money at the same time or buying train tickets.
1. Lock Object
From java1.5 onwards, there are 2 mechanisms to prevent code from being interfered with by concurrent access: synchronized, Reentrantlock. It is important to enclose the unlock operation within the finally clause.
2. Condition Object
Conditional objects are used to manage threads that have acquired a lock but are short of useful work. Example: If in multi-threaded transfer, if the account balance is not enough, you need to wait for other threads to transfer the amount, is the typical use of the conditional object, where a lock, a lock can have one or more related conditional objects. Through the await method of the conditional object, the current thread is blocked and the lock is discarded. A thread calls the await method, which enters the waiting set for that condition, and when the lock is available, the thread cannot unblock immediately, instead it is in a blocking state until another thread calls the Signalall method of the same conditional object.
3.synchronized keywords
Equivalent to an internal lock with only one condition object. Use the wait,notifyall,notify of object, different from the method in the Condition object: Await,signalall,singal
4. Synchronous blocking
5.Volatile Domain
Concurrent access to the domain is secure:
- The domain is final and is accessed after the constructor call is complete;
- Access to the domain, protected by a shared lock
- Domain is volatile
6. Lock Test and timeout
Because the lock method cannot be interrupted, if a thread is interrupted while waiting for a lock, the interrupt thread is blocked until the lock is acquired. If a deadlock occurs, the lock method cannot be terminated. However, when a trylock is called with a timeout parameter, the thread is interrupted during the wait and throws an Interruptedexception exception. This is a very useful feature because the program is allowed to break the deadlock.
7. Read/write Lock
It is useful to use the Reentrantreadwritelock class if many threads read data from one data structure and few threads modify the data.
blocking queues
For many threading problems, you can formalize them in an elegant and secure way by using one or more queues.
Arrayblockqueue
Linkedblockqueue
Thread-Safe collections
Concurrenthashmap
Concurrentskiplistmap
Concurrentskiplistset
Concurrentlinkedqueue
In previous versions of Java, vectors and Hashtable provided thread safety, In 1.2, these classes are deprecated and replaced by ArrayList and HashMap, which are not thread-safe, and the collection library provides different mechanisms to become thread-safe by using a synchronization wrapper
Collections.synchronizedlist (New ArrayList)
Collections.synchronizedmap (New HashMap)
Callable and future
The Futuretask wrapper is a very convenient mechanism to convert callable to future and runnable
Callable<integer> mycomputation = ...
futuretask<integer> task = new futuretask<integer> (mycomputation);
Thread t = new thread (Task);//it is a Runnable
T.start ();
...
Integer result = Task.get ();//it is the future
Actuator
There are newcachedthreadpool,newfixedthreadpool,newsinglethreadexecutor in executors.
There's a scheduled execution, Newscheduledthreadpool.
Synchronous device
1. Countdown Door Bolt Countdownlatch
Once, once the count is 0, it can no longer be reused
Learn notes-java multithreading