Multi-threaded and highly concurrent basis for defining 1-

Processes and Threads

Operating system process is the smallest unit that can be allocated
the smallest unit (different execution path of a program) is an operating system capable of thread scheduling

Three ways to start a thread

1.Thread 2.Runnable 3.Executors

sleep和yield

sleep thread to sleep, let the CPU to other threads continue to run. Time to wake up to a specified
yield the current thread back to the waiting queue, returns to the ready state so that the CPU

wait和notify

The need to synchronize execution, notify and does not release the lock, wait until the lock is released

join

Will be performed in parallel into serial execution

ThreadState thread state

Here Insert Picture Description

Synchronized

1, on an object lock, the lock only in order to get the code to the next step
2, the Hotspot by the object header 64, two out state (mark word) the lock record.

 1.JVM早期,synchronize非常重量级,后来改进锁升级的概念。
 2.一个线程时,mark word仅记录了线程ID,并没有上锁(偏向锁)
 3.如果有线程征用,升级为自旋锁(处于用户态,执行时间较短,线程数比较少,使用自旋锁。执行时间较长,线程较多使用系统锁)
 4.如果在自旋10次后,仍未获得锁,升级为重量级锁,(OS)

3, synchronized (this) is equivalent to the synchronized methods
. 4, synchronized static methods equivalent to synchronize the class
5, reentrant lock: a synchronization method for synchronizing a further method can be called, a thread already owns the lock of an object , when the application again we still get the lock of the object
6, an exception occurs in a program, by default, the lock is released, leading to waiting threads into chaos, resulting in data inconsistencies

Published 25 original articles · won praise 0 · Views 586

Guess you like

Origin blog.csdn.net/RaymondCoder/article/details/105060888