<> Thread unsafe :
The data is negative , The same situation
For example, when it comes to ticket grabbing . The data may be inaccurate in the second stage
public class Unsafe implements Runnable { private static int ticketNum=10;
private boolean flag=true; @Override public void run() {//run may not throws abnormal . while
(flag) { test(); if(ticketNum<0) { break; } } } public static void test() { try
{ Thread.sleep(100);// There will be abnormal data . } catch (InterruptedException e) { // TODO
Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.
currentThread().getName()+"--->"+ticketNum--); } public static void main(String[
] args) { // TODO Auto-generated method stub // A resource System.out.println(Thread.
currentThread().getName());// The thread is main method . Unsafe web=new Unsafe();
// Multiple agents ( That means concurrency ) new Thread(web,"1").start(); new Thread(web,"2").start(); new
Thread(web,"3").start(); new Thread(web,"4").start(); } }
<> Thread synchronization :
Add lock mechanism when accessing . When a thread acquires an exclusive lock on an object , Exclusive resources , Other threads must wait
* Release the lock after use ,
* But there will be a lot of performance problems .
* 1. queue ,2. Lock mechanism
*
* synchronize There are two usages :synchronize Methods and synchronize block
public synchronized void test()
synchronize block :synchronized(object){}
Modified code
public class SynBlock implements Runnable{ private static int ticketNum=10;
private boolean flag=true; @Override public void run() {//run may not throws abnormal . while
(flag) { test3(); } } // Lock in a reasonable range as much as possible . // Double detection , The main purpose is to determine the critical value public void test3() {
if(ticketNum<=0) {// We're thinking about no tickets . flag=false; return; }
// When multiple threads run here , critical value 1, Only one thread enters the synchronization block , Other threads are waiting outside , When the incoming thread executes to
//ticketNum--, therefore ticketNum Just for 0, What's waiting outside can go straight return, This improves performance . synchronized(this) { if(
ticketNum<=0) {// Consider the last ticket left . flag=false; return; } try { Thread.sleep(100);
// There will be abnormal data . } catch (InterruptedException e) { // TODO Auto-generated catch
block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()
+"--->"+ticketNum--); } } // Synchronization block . // Because what we need to judge is ticketnum and flag, But the lock object can only be one
// So look up , That is SynBlock, So that's it this // It can't be locked separately ticketNum no way , because ticketNum It's a change , So it won't lock
// When locking an object , The object to be locked is an invariant quantity . public void test2() { synchronized(this) { if(
ticketNum<=0) { flag=false; return; } try { Thread.sleep(100);// There will be abnormal data . }
catch (InterruptedException e) { // TODO Auto-generated catch block e.
printStackTrace(); } System.out.println(Thread.currentThread().getName()+"--->"+
ticketNum--); } } // Synchronization method // Thread safety , synchronization // What members get is this, Is the object that calls the method . // It's locked this Object's resources .
public synchronized void test1() { if(ticketNum<=0) { flag=false; return; } try
{ Thread.sleep(100);// There will be abnormal data . } catch (InterruptedException e) { // TODO
Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.
currentThread().getName()+"--->"+ticketNum--); } public static void main(String[
] args) { // TODO Auto-generated method stub // A resource System.out.println(Thread.
currentThread().getName());// The thread is main method . SynBlock ts=new SynBlock();
// Multiple agents ( That means concurrency ) new Thread(ts,"1").start(); new Thread(ts,"2").start(); new
Thread(ts,"3").start(); new Thread(ts,"4").start(); } }
Technology