lock lock

java.util.concurrent.locks.lock The mechanism provides better performance than synchronized Code blocks and synchronized Method for a wider range of locking operations . Synchronous code block / Functions of synchronization method lock All have , In addition, it is more powerful and object-oriented .

Use steps :
1. Create one at the member location Reentrantlock object
2. Call before code that may have security problems. Lock Methods in interfaces Lock() Acquire lock
3. Call after code that may have security problems. Lock Method in interface unLock() Release lock

First create RunnableImp class :
package demo09 Thread safety lock lock ; import java.util.concurrent.locks.Lock; import
java.util.concurrent.locks.ReentrantLock; // 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 :lock lock /* java.util.concurrent.locks.lock.lock Interface
Lock The implementation provides more than the use synchronized Methods and statements can obtain a wider range of lock operations Lock Methods in interfaces : void Lock() Acquire lock void
unLock() Release lock Reentrantlock yes Lock An implementation class of interface Use steps : 1. Create one at the member location Reentrantlock object
2. Call before code that may have security problems. Lock Methods in interfaces Lock() Acquire lock 3. Call after code that may have security problems. Lock Method in interface unLock() Release lock */
public class RunnableImp implements Runnable { // Define a ticket source shared by multiple threads private int
ticket=100; //1. Create one at the member location Reentrantlock object Lock L=new ReentrantLock(); // 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){ //2. Call before code that may have security problems. Lock Methods in interfaces Lock() Acquire lock L.lock();
// First judge whether the ticket exists if(ticket>0){ // In order to improve the number of safety problems . Let the program sleep , Output slower try { Thread.sleep(10);
System.out.println(Thread.currentThread().getName()+"--- Selling tickets "+ticket+" Ticket ");
ticket--; } catch (InterruptedException e) { e.printStackTrace(); }finally{
//3. Call after code that may have security problems. Lock Method in interface unLock() Release lock L.unlock();// Whether the program is abnormal or not , Will release the lock } } }
} }
Recreate Ticket class :
package demo09 Thread safety lock lock ; import demo06 Thread safety .RunnableImp; // 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++ of string of compare usage MySQL Basics Commonly used sentence ( Add / delete / modify query )C Language of a Gobang game implementation QT5.9 Use of learning notes QSqlQuery Method of Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control java Polymorphic array of web Front end signature plug-in _signature_pad Plug in implements electronic signature function centos7 install RabbitMqspringboot use redis Experiment 4 Automated test tools - software test