Synchronization of java11_Threads

Synchronization of threads - taking 100 tickets sold in three windows as an example (1) Problem: Double and wrong ticket...
Synchronization of threads - taking 100 tickets sold in three windows as an example

Synchronization of threads - taking 100 tickets sold in three windows as an example

(1) Problem: Double and wrong tickets occurred in the process of selling tickets
(2) Reason: When one thread operates the ticket, the operation is not completed, other threads participate and operate the ticket as well.
(3) Solution: When one thread a is operating on a shared object, other threads cannot participate until thread a is finished, and the other threads
Threads can start manipulating shared objects.
In this case, even though thread a is blocked, it cannot be changed
(4) Using synchronization mechanism in java to solve thread security issues:

Mode 1: Synchronize Code Blocks

Synchronized { //Code that needs to be synchronized (code that manipulates shared data) //Shared data: variables that multiple threads operate on together //cannot contain too much or too few packages }

Synchronization Monitor - commonly known as lock, any object of any class can act as a lock
Requirement: Multiple threads must share the same lock, uniqueness
Supplement 1: Use this as a synchronization monitor in implementing the Runnable interface

class Window1 implements Runnable { /*Only one Window1 object was created, so 100 tickets can be shared*/ private int ticket=100; Object ob=new Object();//Objects of any class can act as locks @Override public void run() { while(true){ synchronized(this) {//this: the only window1 object, no object of another class needs to be created // synchronized(ob) { if (ticket > 0) { System.out.println(Thread.currentThread().getName() + "Ticket selling:" + ticket); ticket--; } else { break; } } } } }

Supplement 2: In the way that inherits the Thread class to create multithreads, use the Thread class.class as the synchronization monitor, and use this with caution

class Window2 extends Thread{ private static int ticket=100; private static Object obj=new Object(); @Override public void run(){ while(true) { // synchronized (obj) { synchronized (Window2.class) {//Objects of class type can also // synchronized(this) catch (InterruptedException e) { e.printStackTrace(); } if (ticket > 0) { System.out.println(getName() + "Ticket selling:" + ticket); ticket--; } } } } } }

Mode 2: Synchronization method

Place the code you need to synchronize in a method, adding synchronized to the method
Summary of synchronization methods:
(1) Synchronization methods still involve synchronization monitors, but there is no need to display declarations
(2) Non-static synchronization method: Synchronization monitor is:
Static synchronization method: Synchronization monitor is the class of the current class itself.

1. Resolve with synchronization method to create multithreads for Runnable interface

class Window3 implements Runnable { /*Only one Window1 object was created, so 100 tickets can be shared*/ private int ticket=100; @Override public void run() { while(true){ show(); } } private synchronized void show(){ if (ticket > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "Ticket selling:" + ticket); ticket--; } } }

2. Synchronization Solution Inherits Thread Class to Create Multithreads

class Window4 extends Thread{ private static int ticket=100; @Override public void run(){ while(true) { show(); } } private static synchronized void show(){//Synchronize Monitor at this time: Window4.class //private synchronized void show(){//synchronization monitor object is not unique, related to new if (ticket > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "Ticket selling:" + ticket); ticket--; } } }

The role of using synchronization mechanisms:

Benefit - Synchronization solves thread security issues
Limitations - When synchronizing code, only one thread can participate, and other threads wait.Equivalent to a single-threaded process, inefficient

2 February 2020, 13:35 | Views: 9652

Add new comment

For adding a comment, please log in
or create account

0 comments