1. Thread Safety
(1) If more than one thread is running at the same time, these threads may run the code at the same time;
(2) The result of each run of the program is the same as the results of single-threaded operation, and the value of the other variables is the same as expected, is thread-safe;
(3) Code Demo:
① Demo Ticket
1 Public classTicketImplementsRunnable {2 //Total 100 votes3 intTicket = 100;4 5 @Override6 Public voidrun () {7 //Mock Sell ticket8 while(true) {9 if(Ticket > 0) {Ten //simulation of the operation of the seat selection One Try { AThread.Sleep (1); -}Catch(interruptedexception e) { - e.printstacktrace (); the } -System.out.println (Thread.CurrentThread (). GetName () + "Selling Tickets:" -+ ticket--); - } + } - } +}
② Test class
1 Public classThreaddemo {2 Public Static voidMain (string[] args) {3 //Create a Ticket object4Ticket Ticket =NewTicket ();5 6 //Create a 3 window7Thread T1 =NewThread (Ticket, "Window 1");8Thread t2 =NewThread (Ticket, "Window 2");9Thread t3 =NewThread (Ticket, "Window 3");Ten One T1.start (); A T2.start (); - T3.start (); - } the}
③ Running results:
(4) The above program has a problem, the wrong ticket 0,-1;
(5) In fact, the thread safety problem is caused by the global variables and static variables;
(6) If the global variable, static variable only read operation in each thread, but no write operation, in general, this global variable is thread-safe;
(7) If there are multiple threads concurrently performing write operations, it is generally necessary to consider thread synchronization, otherwise it may affect thread safety.
2. Thread synchronization (thread-safe handling synchronized)
(1) There are two ways to thread synchronization:
① Synchronous code block;
② Synchronization method
(2) Synchronizing code blocks
① synchronizing code blocks: adding synchronized to code block declarations
synchronized (Lock object) { code that may cause thread safety problems}
The lock object in the ② synchronization code block can be any object, but when multiple threads are used, it is possible to use the same lock object to ensure thread safety.
③ uses the synchronous code block, the ticket class in the ticket sales case to modify the following code, when the use of synchronous code block, the above thread security problem, resolved.
1 Public classTicketImplementsRunnable {2 //Total 100 votes3 intTicket = 100;4 //Defining Lock Objects5Object lock =NewObject ();6 7 @Override8 Public voidrun () {9 //Mock Sell ticketTen while(true) { One //Synchronizing code blocks A synchronized(lock) { - if(Ticket > 0) { - //simulate the operation of a movie pick-and-sit the Try { -Thread.Sleep (10); -}Catch(interruptedexception e) { - e.printstacktrace (); + } - System.out.println (Thread.CurrentThread (). GetName () ++ "Selling Tickets:" + ticket--); A } at } - } - } -}
(3) Synchronization method
① Synchronization method: Add synchronized to the method declaration;
1 Public synchronized void method () {2 code 3 } that may produce thread safety issues
The lock object in the ② synchronization method is this.
③ Use the synchronization method, to the cinema ticket sales case in the ticket class to make the following code changes,
1 Public classTicketImplementsRunnable {2 //Total 100 votes3 intTicket = 100;4 //Defining Lock Objects5Object lock =NewObject ();6 @Override7 Public voidrun () {8 //Mock Sell ticket9 while(true){Ten //Synchronization Method One method (); A } - } - the //synchronization method, lock object this - Public synchronized voidmethod () { - if(Ticket > 0) { - //simulation of the operation of the seat selection + Try { -Thread.Sleep (10); +}Catch(interruptedexception e) { A e.printstacktrace (); at } -System.out.println (Thread.CurrentThread (). GetName () + "Selling Tickets:" + ticket--); - } - } -}
(4) Static synchronization method
① Static Synchronization method: Add static synchronized to the method declaration;
The lock object in the ② static synchronization method is the class name. Class.
Public Static synchronized void method () {Code that may cause thread safety problems}
in fact, thread-safety issues are global variables and static variables
00102_ thread safety and thread synchronization