To address multithreading security issues
Prior to Java 5.0, the mechanisms used to reconcile access to shared objects were only synchronized and volatile.
Java 5.0 adds some new mechanisms, but is not an alternative to built-in locks, but is an optional advanced feature when built-in locks do not apply.
Implicit lock
Synchronized
- Synchronous code block;
- The synchronization method.
Show Locking Lock
After JDK 1.5: The sync lock lock needs to be locked through the lock () method, and the lock must be released through the Unlock () method.
The Reentrantlock implements the Lock interface and provides the same mutex and memory visibility as the synchronized. But compared to the synchronized provides a higher flexibility for handling locks.
Multithreading security issues without locks Demo:
PackageCom.company; Public classTestlock { Public Static voidMain (string[] args) {Ticket Ticket=NewTicket (); NewThread (Ticket, "window number 1th"). Start (); NewThread (Ticket, "window number 2nd"). Start (); NewThread (Ticket, "window number 3rd"). Start (); }}classTicketImplementsrunnable{Private intTick = 100; @Override Public voidrun () { while(true){ if(Tick > 0){ Try{Thread.Sleep (200); } Catch(Interruptedexception e) {} System.out.println (Thread.CurrentThread (). GetName ()+ "Complete the ticket, the remaining ticket is:" +--tick); } } }}
See the problem (part of the interception result):
3rd window to complete the ticket, the remaining ticket is: 1th window to complete the ticket, theremaining tickets: 82nd window to completethe ticket, the remaining tickets: 83rd window to complete the ticket, the remaining tickets: 82nd window to complete the ticket, the remaining tickets:7 1th window to complete the ticket, theremaining tickets: 73rd window to complete the ticket, the remaining ticket is:71th window to complete the ticket, the remaining ticket is:6 2nd window to complete the ticket, theremaining ticket is:53rd window to complete the ticket, the remaining ticket is:43rd window to complete the ticket , the rest of the ticket is: 31st window to complete the ticket, the remaining ticket is: 32nd window to complete the ticket, the remaining ticket is: 33rd window to complete the ticket, theremaining ticket is:21th window to complete the ticket, the remaining ticket is:02nd window to complete the ticket, the remaining votes:1
Now we use lock to do the processing:
PackageCom.company;ImportJava.util.concurrent.locks.Lock;ImportJava.util.concurrent.locks.ReentrantLock; Public classTestlock { Public Static voidMain (string[] args) {Ticket Ticket=NewTicket (); NewThread (Ticket, "window number 1th"). Start (); NewThread (Ticket, "window number 2nd"). Start (); NewThread (Ticket, "window number 3rd"). Start (); }}classTicketImplementsRunnable {Private intTick = 100; Private Lock lock = new Reentrantlock (); @Override Public voidrun () { while(true) { Try{ lock.lock (); if(Tick > 0) { Try{Thread.Sleep (200); } Catch(Interruptedexception e) {} System.out.println (Thread.CurrentThread (). GetName () + "Complete the ticket, the remaining ticket is:" +--tick); } } finally{ lock.unlock (); } } }}
Look at the results (and, of course, part of it), perfect for multithreading security issues:
2nd window to complete the ticket, the remaining ticket is: 20Window 2nd completes the ticket sales, the remaining ticket is:19Window 2nd completes the ticket sales, the remaining ticket is:18Window 2nd completes the ticket sales, the remaining ticket is:17Window 2nd completes the ticket sales, the remaining ticket is:16Window 2nd completes the ticket sales, the remaining ticket is:15Window 2nd completes the ticket sales, the remaining ticket is:14Window 2nd completes the ticket sales, the remaining ticket is:13Window 2nd completes the ticket sales, the remaining ticket is:12Window 2nd completes the ticket sales, the remaining ticket is:11Window 2nd completes the ticket sales, the remaining ticket is:10Window 2nd completes the ticket sales, the remaining ticket is:9Window 2nd completes the ticket sales, the remaining ticket is:8Window 2nd completes the ticket sales, the remaining ticket is:7Window 2nd completes the ticket sales, the remaining ticket is:6Window 2nd completes the ticket sales, the remaining ticket is:5Window 2nd completes the ticket sales, the remaining ticket is:4Window 2nd completes the ticket sales, the remaining ticket is:3Window 2nd completes the ticket sales, the remaining ticket is:2Window 2nd completes the ticket sales, the remaining ticket is:1Window 2nd completes the ticket sales, the remaining ticket is:0
Java Multithreading--synchronization lock