Thread safety

There is no thread safety problem with single thread

Multithreaded programs do not access shared data , No problem

Multithreading accessing shared data will cause thread safety problems

// Case of realizing ticket purchase

// There is a thread safety problem in ticket selling : Sold non-existent tickets and duplicate tickets

// A scheme of using thread safety : Using synchronized code blocks

/* format :

          synchronized( Synchronous lock ){

                      Code requiring synchronous operation

         }

be careful : The lock object in the synchronization code can be any object , However, we must ensure that the lock object used by multiple threads is the same Lock object function ; Lock the synchronization code , Let only one thread execute in a synchronized code block */

First create RunnableImp class
package demo06 Thread safety ; // Case of realizing ticket purchase // There is a thread safety problem in ticket selling : Sold non-existent tickets and duplicate tickets // A scheme of using thread safety : Using synchronized code blocks
/* format ; synchronized( Synchronous lock ){ Code requiring synchronous operation }
be careful : The lock object in the synchronization code can be any object , However, we must ensure that the lock object used by multiple threads is the same Lock object function ; Lock the synchronization code , Let a thread execute only one block of code */
public class RunnableImp implements Runnable { // Define a ticket source shared by multiple threads private int
ticket=100; // Create a lock object Object obj=new Object(); // Define thread tasks : Buy a ticket @Override public
void run() { // Use the dead loop to repeat the ticket selling while(true){ // Synchronous code block synchronized(obj){ // First judge whether the ticket exists
if(ticket>0){ // In order to improve the number of safety problems . Let the program sleep try { Thread.sleep(10); } catch
(InterruptedException e) { e.printStackTrace(); }
System.out.println(Thread.currentThread().getName()+"--- Selling tickets "+ticket+" Ticket ");
ticket--; } } } } }
Creating Ticket class :
package demo06 Thread safety ; // Simulated ticket buying case , Sell shared tickets public class Ticket { public static
void main(String[] args) { // Create the implementation class object of the interface RunnableImp run =new RunnableImp();
// establish Thread Class object , Transfer in construction method Runnable Interface object Thread t0=new Thread(run); Thread t1=new
Thread(run); Thread t2=new Thread(run); // Open thread t0.start(); t1.start();
t2.start(); } }
result :

Technology
©2019-2020 Toolsou All rights reserved,
【C++ Must see for entry 】C++ from 0 reach 1 Introductory programming axios Interceptor packaging and use Spring Boot Interview must ask : Automatic configuration principle VMware 16 install centos 7 Detailed tutorial C Language data structure - Sequence table delete duplicates V2.0.0 The 12th Blue Bridge Cup c++b Group personal problem solving On sending data from serial port single chip microcomputer to upper computer centos7 install RabbitMqjava Polymorphic array of opencv-python Fourier transform and inverse transform