The critical resource issues multithreaded Java

The reason the critical resource issues: one thread at a critical time for access to resources, not enough time to completely modify the value of critical resources, critical resources to take other threads to access, resulting in multiple threads access the same resource. Intuitive print results showed chaotic order.

Solution: lock

Locks using static method, the object lock by non-static methods.

1. The synchronization code segment: synchronized () {...}
2. synchronization method: a method using a modified synchronized keyword
3. explicit synchronization lock ReentrantLock

An outer lock pool that is described lock wait state

Method a: synchronization code segment: synchronized () {...}

public  class SourceConflict {
     public  static  void main (String [] args) {
         // instantiate conductor 4, analog conductor 4 with four threads 
        
        the Runnable R & lt = () -> {
             the while (TicketCenter.restCount> 0 ) {
                 the synchronized ( "" ) {
                     IF (TicketCenter.restCount <= 0 ) {
                         return ; 
                    } 
                    System.out.println (Thread.currentThread (). getName () + "buy a ticket, the remaining" + --TicketCenter.restCount + " tickets " ); 
                }
            } 
        };
        
        // analog conductor 4 with four threads 
        the Thread Thread1 = new new the Thread (R & lt, "Thread1" ); 
        the Thread Thread2 = new new the Thread (R & lt, "Thread2" ); 
        the Thread thread3 = new new the Thread (R & lt, "Thread -3 " ); 
        the thread thread4 = new new the thread (R & lt," thread4 " ); 
        
        // open thread 
        thread1.start (); 
        thread2.start (); 
        thread3.start (); 
        thread4.start (); 
        
    }     
} 

// achieve four conductor common ticketing, resource sharing, non-independent
 //Lambda expressions anonymous inner or internal trapping local variables must be explicitly declared as final or final type of practical effect, captured or static instance variable is not restricted 
class TicketCenter {
     public  static  int restCount = 100 ; 
}

Method two: synchronization method, using keywords for synchronized modification method

. 1  public  class SourceConflict2 {
 2      public  static  void main (String [] args) {
 . 3          // instantiate conductor 4, analog conductor 4 with four threads 
. 4          
. 5          the Runnable R & lt = () -> {
 . 6              the while (TicketCenter.restCount > 0 ) {
 . 7                  sellTicket ();
 . 8              }
 . 9          };
 10          
. 11          // analog conductor 4 with four threads 
12 is          the thread Thread1 = new new the thread (R & lt, "Thread1" );
 13 is          the thread Thread2 = new new Thread(r, "thread-2");
14         Thread thread3 = new Thread(r, "thread-3");
15         Thread thread4 = new Thread(r, "thread-4");
16         
17         //开启线程
18         thread1.start();
19         thread2.start();
20         thread3.start();
21         thread4.start();
22         
23     }
24     
25     private synchronized static void sellTicket() {    
26         if (TicketCenter.restCount <= 0) {
27              return ;
 28          }
 29          System.out.println (Thread.currentThread () getName () + "buy a ticket, the remaining" + --TicketCenter.restCount + "tickets." );
 30      }
 31  }
 32  
33 is  class TicketCenter {
 34 is      public  static  int restCount = 100 ; 
 35 }

Method 3: Use explicit synchronization lock ReentrantLock

. 1  Import java.util.concurrent.locks.ReentrantLock;
 2  
. 3  public  class SourceConflict3 {
 . 4      public  static  void main (String [] args) {
 . 5          // instantiate conductor 4 with four threads analog four conductor
 . 6          
. 7          / / explicit lock 
. 8          of ReentrantLock = lock new new of ReentrantLock ();
 . 9          the Runnable R & lt = () -> {
 10              the while (TicketCenter.restCount> 0 ) {
 . 11                  Lock.lock ();
 12 is                  IF (TicketCenter.restCount <= 0 ) {
 13                     return ;
 14                  }
 15                  System.out.println (Thread.currentThread () getName () + "buy a ticket, the remaining" + --TicketCenter.restCount + "tickets." );
 16                  lock.unlock ();
 17              }
 18          };
 . 19          
20 is          // analog 4 with four threads conductor 
21 is          the thread Thread1 = new new the thread (R & lt, "Thread1" );
 22 is          the thread Thread2 = new new the thread (R & lt, "Thread2" );
 23 is          = thread3 the Thread new new the Thread (R & lt, "thread3" );
 24         Thread thread4 = new Thread(r, "thread-4");
25         
26         //开启线程
27         thread1.start();
28         thread2.start();
29         thread3.start();
30         thread4.start();
31         
32     }    
33 }
35 class TicketCenter{
36     public static int restCount = 100; 
37 }

 

 

Guess you like

Origin www.cnblogs.com/ggrrbb/p/12289687.html