catalogue
1.2 concept of process and thread
1.3.2 difference between wait and sleep
1.4 concurrency and parallelism
1.4.5 user thread and daemon thread
2.2.2 the conductor buys tickets
2.4 difference between synchronized and lock
1 what is JUC?
1.1 introduction to JUC
In Java, JUC is Java uitl. Short for concurrent toolkit. This is a toolkit for processing threads jdk1 After 5.
1.2 concept of process and thread
Process: a process is a running activity of a program on a data set in a computer. It is the basic unit for resource allocation and scheduling of the system and the basis of the structure of the operating system. A process is an entity of a program.
Thread: thread is the smallest unit that the operating system can schedule operations. It is included in the process and is the actual operation unit in the process. A thread refers to a single sequential control flow in a process. Multiple threads can be concurrent in a process, and each thread executes different tasks in parallel.
1.3 thread status
1.3.1 thread status:
new, Runnable, Blocked, Waiting, TimeWaiting, Terminated
1.3.2 difference between wait and sleep
(1) sleep is the static method of Thread, and wait is the method of Object, which can be called by any instance Object.
(2) sleep does not release the lock, nor does it need to occupy the lock. wait will release the lock, but the premise of calling it is that the current thread occupies the lock (that is, the code should be in synchronized).
(3) They can all be interrupted by interrupt
1.4 concurrency and parallelism
1.4.1 serial mode
Serial mode: indicates that all tasks are executed in sequence.
1.4.2 parallel mode
Parallel mode: obtain multiple tasks at the same time and execute these obtained tasks at the same time. Parallel mode is equivalent to a long queue.
1.4.3 concurrency
Concurrency: multiple threads access a resource at the same time, such as Spring Festival transportation ticket grabbing and second kill rush buying
1.4.4 parallel
Parallel: multiple tasks are executed together and then summarized.
Tube side: Monitor (lock)
Monitor: it is a synchronization mechanism to ensure that only one thread accesses the protected data or code at the same time.
1.4.5 user thread and daemon thread
User thread: Custom thread (the main program ends, the user thread is still running, and the JVM survives)
public class Main { public static void main(String[] args) { Thread a = new Thread(() -> { System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().isDaemon()); while (true){ } },"a"); a.start(); System.out.println(Thread.currentThread().getName()+"over"); } }
Daemon thread: (there is no user thread, all are daemon threads, and the JVM ends running)
package com.workspace.main; public class Main { public static void main(String[] args) { Thread a = new Thread(() -> { System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().isDaemon()); while (true){ } },"a"); a.setDaemon(true); //Set daemon thread a.start(); System.out.println(Thread.currentThread().getName()+"over"); } }
2 LOCK interface
2.1 synchronized
2.1.1 synchronized keyword
synchronized is an important keyword in Java. It is a kind of synchronization lock.
It modifies the following objects:
1. Modify a code block. The modified code block is called synchronization statement block. Its scope of action is the code enclosed in braces {}, and the object of action is the object calling the code block;
2. Modify a method. The modified method is called synchronous method. Its scope of action is the whole method, and the object of action is the object calling the method;
3. Modify a static method. Its scope of action is the whole static method, and the object of action is all objects of this class;
4. Modify a class. Its scope of action is the part enclosed in parentheses after synchronized. The main object of action is all objects of this class.
2.2.2 the conductor buys tickets
//1. Create a resource class to define attributes and operation methods class Ticket{ // Number of votes private int number = 30; public synchronized void sale(){ //Judge whether there are tickets if (number > 0) { System.out.println(Thread.currentThread().getName()+"Sold, left"+ --number); } } } public class SaleTicket { public static void main(String[] args) { // Create a ticket object Ticket ticket = new Ticket(); // Create 3 threads to sell tickets respectively new Thread(() -> { for (int i = 0; i < 30; i++) { ticket.sale(); } },"A").start();; new Thread(() -> { for (int i = 0; i < 30; i++) { ticket.sale(); } },"B").start();; new Thread(() -> { for (int i = 0; i < 30; i++) { ticket.sale(); } },"C").start(); } }
2.2 LOCK interface
Different from the built-in synchronization and monitor, LOCK is a class that can realize synchronous access through classes, and multiple interfaces realize classes: reentrant LOCK, etc
Code definition of reentrant lock: ReentrantLock
private final ReentrantLock lock = new ReentrantLock(true);
Realize ticket selling operation with lock
//1. Create a resource class to define attributes and operation methods class LTicket{ // Number of votes private int number = 30; // Create a reentrant lock private final ReentrantLock lock = new ReentrantLock(); // Ticket buying method public void sale(){ // Lock lock.lock(); try{ //Judge whether there are tickets if (number > 0) { System.out.println(Thread.currentThread().getName()+"Sold, left"+ --number); } } finally{ // Unlock lock.unlock(); } } } public class LsaleTicket { public static void main(String[] args) { // Create a ticket object LTicket ticket = new LTicket(); // Create 3 threads to sell tickets respectively new Thread(() -> { for (int i = 0; i < 30; i++) { ticket.sale(); } },"A").start();; new Thread(() -> { for (int i = 0; i < 30; i++) { ticket.sale(); } },"B").start();; new Thread(() -> { for (int i = 0; i < 30; i++) { ticket.sale(); } },"C").start(); } }
2.3 Lock interface
public interface Lock { void lock(); void lockInterruptibly() throws InterruptedException; boolean tryLock(); boolean tryLock(long time, TimeUnit unit) throws InterruptedException; void unlock(); Condition newCondition(); }
2.4 difference between synchronized and lock
Synchronized is a built-in keyword in java, while lock is a class that can realize synchronous access and is richer than the methods in synchronized
synchronized will not release the lock manually, but lock needs to release the lock manually (deadlock will occur if it is not unlocked, and the lock needs to be released in the finally block)
The lock thread waiting for the lock will be interrupted accordingly, while the synchronized thread will not respond and will only wait all the time
Through Lock, you can know whether you have successfully obtained the Lock, but synchronized cannot
When multiple threads compete, Lock can improve the efficiency of reading operations by multiple threads