######## - Multi-threaded Outline - ############################
1. The way to achieve thread: [inheritance Thread class that implements Runnable interface]
2. static proxy design pattern
3.jdk1.8 lambda expression
4. thread state
5. thread priority
6. daemon thread.
Common method 7.Thread thread class.
Synchronous, asynchronous threads 8.
9. concurrent containers.
10. deadlock.
######## - multithreading knowledge points - ##############################
1. To achieve a thread way: [inheritance Thread, implement Runnable]
① inherited Thread, implement run () method.
Thread Use: = new new myThread the MyThread the MyThread (); myThread.start ();
Thread features:
② to achieve Runnable interface, and override the run () method [used]
Runnable use: new new the Thread (new new MyRunnable ()) Start ();.
Runnable features: easy sharing of resources | Avoid single inheritance |
③ a Callable and override call ( ) method advanced concurrent juc [section]
2. Static Proxy design pattern
- Public interface:
- Real Role: implement the common interface
- Acting roles: implement the common interface, incoming real role, calling for the method according to the real role.
3.jdk1.8 lambda expression
- Push to: [static inner classes - "local inner class - the" anonymous inner classes. - "jdk1.8 lambda expression]
- Simplification of anonymous inner classes. For the simple thread body. new Thread (() -> { System.out.println ( " thread run method implemented body member.")}) Start ();.
* Case the lambda expression:
public class Lambda1 {
public static void main (String [ ] args) {
the Add the Add = (A, B) -> A + B;
int = A add.AddTO (10, 23 is);
System.out.println (A);
}
}
interface Add{ public int AddTO(int a,int b); }
4. Thread State
* Newborn state: the Thread the Thread = new new the Thread ();
* ready state: thread.start (); unblocked; yield () [] into the ready state; jvm call to;
* Operating status: get CPU resources.
* Blocking installed state: sleep [footprint -> time coming home and enter the ready state]; wait [do not take up resources]; join [jump the queue thread, waiting for other threads finish before they can implement their own thread]; read \ wait [operating systems go call];
* death state: stop; [not recommended]; destroy; [not recommended]; Description: Normal finish or by controlling the external thread to stop.
The thread priority
- NORM_PRIORITY 5默认 | MIN_PRIORITY 1 | MAX_PRIORITY 10
- Get thread priority: Thread.currentHread () getPriority ();.
- Set Priority: thread.setPriority (Thread.MIN_PRIORITY);
6. daemon thread [].
* Set the thread as a daemon thread
the Thread the Thread new new T = (new new God ());
t.setDaemon (to true); // set daemon thread
t.start ();
Common method 7.Thread thread class.
- Thread.currentThread () isAlive ();. // is alive
- Thread.currentThread (): the current thread
8. thread synchronization, asynchronous
* Use the synchronized approach to concurrency control:
synchronization method public syncronizde void method (int ages) {...}
sync block: synchronized (obj) {...}
- ① ② practice exercises buy movie tickets buy tickets
9. concurrent containers
- CopyOnWriteArrayList container which comes concurrent synchronization lock: CopyOnWriteArrayList list = new CopyOnWriteArrayList ();
10. Deadlock
- Excessive synchronization, there will be a deadlock. General synchronization hold more than one object lock.
- Avoid deadlock: not in the same code block, a plurality of objects while holding the lock.
- Write a deadlock case. Such as: lipstick and a mirror.
11. concurrent collaboration, producer consumer model.
* Tube-law [container]: Multithreaded creator | multithreading consumers | buffer | data [bread]
* lights Act [identifier]:
* in the Java Object provides:
the wait () [causes the current thread to wait, call notify or notifyAll will release], wait (long timeOut) [lead to] the current thread to wait for a certain time,
notify () [], notifyAll () []
12. Advanced Topics multithreading
Multi-threading, timed scheduling: Timer TimerTask
quartz [task] framework
[Command] HappenBefore rearrangement
-------------------------
volatile [used to ensure synchronization of data, that is visibility. But does not guarantee atomicity, lightweight synchronized. volatile avoided instruction rearrangement]
----------------
DCL single factor authentication mode [+] volatile avoid reordering instruction
-------------- -
ThreadLocal [independent space can be allocated for each thread. There are get / set / initialVaule Method]
InheritableThreadLocal [inherited context of data to find the starting point]
----------------------
reentrant lock principle [achieve] can continue to use the calculator +
-------------
CAS [compare and swap AtomicInteger stock = new AtomicInteger (5) ; ]