<>java Locking mode

1,synchronized mode ( Heavyweight lock )

Locking mode :synchronized(object) Incoming object , Different objects represent different locks , You can create new objects outside the thread .
public class SellCinemaTicketThread implements Runnable { static int num = 100;
Object object = new Object(); @Override public void run() { while (true) {
synchronized (object) { if (num > 0) { System.out.println(Thread.currentThread()
.getName() + " Buy a ticket ---" + num); num--; } } } } }
It can also be passed in as a modification of a method
public class SellCinemaTicketThread1 implements Runnable { static int num = 100
; @Override public void run() { sell(); } private synchronized void sell() {
while (true) { if (num > 0) { System.out.println(Thread.currentThread().getName(
) + " Buy a ticket ---" + num); num--; } } } }
2,Lock( than synchronized Be lightweight )

* New lock object Lock l = new ReentrantLock();
* Lock l.lock()
* Unlock l.unlock() public class TestLockSellTicket implements Runnable { static
int num = 100; Lock l = new ReentrantLock(); @Override public void run() { while
(true) { l.lock(); if (num > 0) { try { Thread.sleep(100); System.out.println(
" Buy a ticket ---" + num); num--; } catch (InterruptedException e) { e.printStackTrace();
} finally { l.unlock(); } } } } }
3,wait() notify() notifyAll()
/* wait() notify() notifyAll() yes Object Method on , only Object This method can only be called when it is an object monitor notify()
Wake up a thread on the same object monitor wait(Long timeout) Incoming value , Indicates that if you have not been awakened for more than a certain time, you will wake up automatically wait and sleep difference :
sleep() It can be used in and out of synchronization , Thread encountered sleep Do not release the lock , It's time to continue down wait() Can only be used in synchronization , Called by object monitor ,
Thread entry wait Release the lock when in the state */ public class TestWaitAndNotify { public static void main(
String[] args) { Object obj = new Object(); new Thread(new Runnable() {
@Override public void run() { synchronized (obj) { System.out.println(" Start waiting ...."
); try { obj.wait(); } catch (InterruptedException e) { e.printStackTrace(); }
System.out.println(" Wait for the end ...."); } } }).start(); new Thread(new Runnable() {
@Override public void run() { synchronized (obj) { // Sleep for two seconds and then wake up the thread try { Thread.
sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } obj.
notify(); } } }).start(); } }
<> Buy tickets and lock them

SellCinemaTicketThread1 class
public class SellCinemaTicketThread1 implements Runnable { static int num = 100
; @Override public void run() { sell(); } private synchronized void sell() {
while (true) { if (num > 0) { System.out.println(Thread.currentThread().getName(
) + " Buy a ticket ---" + num); num--; } } } }
TestSellCinemaTicket Ticket category Multiple threads sell tickets at the same time
/* Keep in sync : Code block locking synchronized ( Any object is an object lock ) { Objects that need to be synchronized } Method locking Add before method return synchronized
private synchronized void sell() { Synchronization code } */ public class TestSellCinemaTicket {
public static void main(String[] args) { // Runnable r = new
SellCinemaTicketThread(); Runnable r = new SellCinemaTicketThread1(); Thread t1
= new Thread(r); Thread t2 = new Thread(r); Thread t3 = new Thread(r); t1.start(
); t2.start(); t3.start(); } }

Technology