Java Multithreading foundation (a)

    1. Thread synchronization (synchronization lock)
    1. Synchronous communication between threads

5. Thread synchronization (synchronization lock) When is synchronization required

  • When multiple threads are running concurrently, we hope that the CPU will not switch to another thread during the execution of a certain code. In this case, synchronization is needed.
  • If two pieces of code are synchronized, only one piece of code can be executed at a time

Synchronized code block

  • Use the synchronized keyword and a lock object to define a piece of code called a synchronized block
  • Multiple synchronized blocks of code are synchronized if they use the same lock object

Let’s do a Demo to understand this

// If there are multiple threads running at the same time, there may be two threads that are qualified to execute at the same time. Public class Demo4 {public static void main(String[] args) {public static void main(String[] args) {TicketThread t1=new TicketThread("A"); TicketThread t2=new TicketThread("B"); TicketThread t3=new TicketThread("C"); TicketThread t4=new TicketThread("D"); // Start thread t1.start(); t2.start(); t3.start(); t4.start(); }} class TicketThread extends Thread{static int kets=100; Public TicketThread(String name) {super(name); // TODO Auto-generated constructor stub } @Override public void run() { while (true) { try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //synchronized: parameter: a lock. The lock can be any object. If multiple objects need to be synchronized, use the same lock. Synchronized ("nihao") {// synchronized ("nihao") { If (thread.out.println (thread.currentThread ().getname ()+" "+tikets--); }else{ break; } } } } }Copy the code

6. When do synchronous threads need to communicate

  • When multiple threads execute concurrently, the CPU switches threads randomly by default
  • If we want them to execute regularly, we can use communication, such as printing once per thread

How to communicate

  • If you want the thread to wait, call wait();
  • If you want to wake up a waiting thread, call notify();
  • These two methods must be executed in synchronized code and invoked using a synchronized lock object.

Let’s do a Demo to understand this

Public class Demo1 {public static void main(String[] args) {final Shuchu sc=new Shuchu(); new Thread(){ @Override public void run() { while (true) { try { sc.scw1(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); new Thread(){ @Override public void run() { while (true) { try { sc.scw2(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); }} /* Wait () is used to call wait(); * What is the difference between sleep() and wait()? * 1. The sleep() method does not have to pass an argument. If there is no argument, wait. Class Shuchu{private int flag=1; class Shuchu{private int flag=1; Public void scw1() throws InterruptedException{synchronized (this) {while(flag! =1){ this.wait(); } System.out.print("1"); System.out.print("2"); System.out.print("3"); System.out.print("4"); System.out.println("5"); flag=2; this.notify(); Public void scw2() throws InterruptedException{synchronized (this) {while(flag! =2){ this.wait(); } System.out.print("A"); System.out.print("B"); System.out.print("C"); System.out.print("D"); System.out.println("E"); flag=1; this.notify(); // Randomly wake up another thread}}}Copy the code

What is the difference between sleep() and wait()?

  • 1. The sleep() method does not have to pass an argument. If no argument is passed, wait, or if it is passed, wait

  • 2. The sleep() method does not release the lock during synchronization