1. Synchronized code block
grammar
Synchronized (lock){ // Code segments requiring access to critical resources }
_Lock must be an object_
- 1) When the program goes to the code segment, it uses the lock to lock the critical resources. At this time, other threads can not execute the code in the code segment, but can only wait outside the lock.
- 2) The code in the code segment will be unlocked automatically after execution. Then the rest of the threads start scrambling for cpu time slices
- 3) Make sure that different threads see the same lock, otherwise synchronizing blocks of code is meaningless.
Example:
package cn.cc.day22; /** * @author CC * Thread Synchronization Code Block */ public class Ticket { public static void main(String[] args) { //Only one new object TicketThread ticketThread = new TicketThread(); Thread td3 = new Thread(ticketThread); td3.setName("Thread 3"); Thread td1 = new Thread(ticketThread); td1.setName("Thread 1"); Thread td2 = new Thread(ticketThread); td2.setName("Thread 2"); td1.start(); td2.start(); td3.start(); } } class TicketThread implements Runnable{ //votes private int ticket = 5; public void run() { for (int i = 0; i < 100 ; i++) { //Using Thread Synchronization: Synchronizing Code Blocks //Synchronize the code in the code block, not allowing multiple threads to execute simultaneously //Any object can be used as a symbol of synchronization //Using this for locks, you can only create an object of the current class synchronized (this) { //Analysis: this represents the object of TicketThread //If there are threads accessing the code in the synchronization block, set this to 1 //When another thread accesses the code in the synchronous code block, it will determine whether it is the same this, and if it is the same this, it will determine whether it is 1. //If it is 1, the thread is blocked and the code in the synchronization block is not allowed to execute. //When the first thread finishes execution of the code in the synchronized code block, set this to 0, indicating that the thread in the blocked state is in the ready state. //Synchronized code if (ticket>0) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":" +"Sell No."+ticket+"Zhang ticket,Left"+--ticket+"Zhang ticket"); } } } } }
Analysis of object locks:
- this represents the object of the current class
- If a thread accesses the code in the synchronous code block, set this to 1, and another thread accesses the code in the synchronous code block, it will determine whether it is the same this, if it is the same this, it will determine whether it is 1. If it is 1, the thread is blocked and it is not allowed to execute the code in the synchronous code block.
- When the first thread finishes execution of the code in the synchronized code block, set this to 0, indicating that the thread in the blocked state is in the ready state.
2. Thread synchronization method
package cn.cc.day22; /** * @author CC * Thread synchronization method */ public class Ticket2 { public static void main(String[] args) { TicketThread2 t = new TicketThread2(); Thread td1 = new Thread(t); Thread td2 = new Thread(t); Thread td3 = new Thread(t); td1.start(); td2.start(); td3.start(); } } class TicketThread2 implements Runnable{ private int ticket = 5; @Override public synchronized void run() { for (int i = 0; i < 100; i++) { if (ticket > 0) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":" +"Sell No."+ticket+"Zhang ticket,Left"+--ticket+"Zhang ticket"); } } } }
3. Synchronization lock:
package cn.cc.day22; import java.util.concurrent.locks.ReentrantLock; /** * @author CC * Synchronization lock */ public class Ticket3 { public static void main(String[] args) { LockThread lt = new LockThread(); Thread td1 = new Thread(lt); Thread td2 = new Thread(lt); Thread td3 = new Thread(lt); td1.start(); td2.start(); td3.start(); } } class LockThread implements Runnable{ private int ticket = 5; //Declare Lock Objects ReentrantLock myLock = new ReentrantLock(); @Override public void run() { myLock.lock();//Lock up for (int i = 0; i < 100; i++) { if (ticket>0) { try { Thread.sleep(100);//The locks are still in effect during sleep, and the locks are held to sleep. } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":" +"Sell No."+ticket+"Zhang ticket,Left"+--ticket+"Zhang ticket"); } } myLock.unlock();//Unlock } }
4. Deadlock
package cn.cc.day22; /** * @author CC * deadlock */ public class DeadLock { public static void main(String[] args) { DeadLock d = new DeadLock(); Thread td1 = new DeadThread(); Thread td2 = new DeadThread(); td1.start(); td2.start(); } } class DeadThread extends Thread{ //Declare two objects as locked objects static Object o1 = new Object(); static Object o2 = new Object(); //Locked Mark static boolean flag = true; public void run() { if (flag) { //Lock O1 synchronized (o1) { System.out.println(Thread.currentThread().getName()+"Get the first object o1," + "Waiting for the second lock object o2"); flag = !flag; try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (o2) { System.out.println(Thread.currentThread().getName()+"Get the lock object o1 and o2," + "Release two objects"); } } }else { //Lock o2 synchronized (o2) { System.out.println(Thread.currentThread().getName()+"Get the first object o2," + "Waiting for the second lock object o1"); flag = !flag; try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (o1) { System.out.println(Thread.currentThread().getName()+"Get the lock object o1 and o2," + "Release two objects"); } } } } }