1、使用关键字:synchronized java
一、synchronized能够修饰方法,(其实也是锁的是当前方法的对象),也能够修饰代码块,修饰代码也须要传入一个对象。 synchronized(object){}程序员
二、两种方式都是获取的是synchronized修饰对象的monitor锁!这才是真正的锁!ide
三、synchronized所修饰的锁不能够为null!线程
public class Main{ private static int commonTag = 0; public static void main(String[] args) { Runnable runnable = new Runnable() { @Override public void run() { while (true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } add(Thread.currentThread().getId()); } } }; Thread t1 = new Thread(runnable); Thread t2 = new Thread(runnable); Thread t3 = new Thread(runnable); Thread t4 = new Thread(runnable); t1.start(); t2.start(); t3.start(); t4.start(); } public static synchronized void add(long ID){ System.out.println("当前线程ID:"+ID+"执行结果="+commonTag++); } // public static void add(long ID){ // synchronized(object){ //也须要传入一个对象 // System.out.println("当前线程ID:"+ID+"执行结果="+commonTag++); // // } // } }
当前线程ID:10执行结果=0 当前线程ID:11执行结果=1 当前线程ID:12执行结果=2 当前线程ID:13执行结果=3 当前线程ID:10执行结果=4 当前线程ID:12执行结果=5 当前线程ID:13执行结果=6 当前线程ID:11执行结果=7 当前线程ID:10执行结果=8 当前线程ID:13执行结果=9 当前线程ID:12执行结果=10 当前线程ID:11执行结果=11 当前线程ID:10执行结果=12 当前线程ID:13执行结果=13 当前线程ID:12执行结果=14 当前线程ID:11执行结果=15
若是不用则结果以下:code
当前线程ID:12执行结果=3 当前线程ID:10执行结果=1 当前线程ID:13执行结果=2 当前线程ID:11执行结果=0 当前线程ID:12执行结果=5 当前线程ID:11执行结果=7 当前线程ID:13执行结果=6 当前线程ID:10执行结果=4 当前线程ID:11执行结果=8 当前线程ID:10执行结果=9 当前线程ID:13执行结果=9 当前线程ID:12执行结果=8
2、死锁的缘由和如何避免!对象
对于程序员须要关注两个方向:一个是内存不足引发的死锁,另外一个是交叉锁,各持对方的排它锁致使的死锁,谁都不愿放!内存
public class Main{ private static int commonTag = 0; private static Object object1 = new Object(); private static Object object2 = new Object(); public static void main(String[] args) { Runnable runnable = new Runnable() { @Override public void run() { while (true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } add(Thread.currentThread().getId()); } } }; Runnable runnable2 = new Runnable() { @Override public void run() { while (true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } add2(Thread.currentThread().getId()); } } }; Thread t1 = new Thread(runnable); Thread t2 = new Thread(runnable); Thread t3 = new Thread(runnable2); Thread t4 = new Thread(runnable2); t1.start(); t2.start(); t3.start(); t4.start(); } public static void add(long ID){ synchronized(object1){ synchronized(object2){ System.out.println("当前线程ID:"+ID+"执行结果="+commonTag++); } } } public static void add2(long ID){ synchronized(object2){ synchronized(object1){ System.out.println("当前线程ID:"+ID+"执行结果="+commonTag++); } } } }
3、LOCK(java.util.concurrent.locks)get
一、读写锁?it
二、自旋锁io
import java.util.concurrent.locks.ReentrantLock; public class Main{ private static int commonTag = 0; private static ReentrantLock lock = new ReentrantLock(); private static Object object = new Object(); public static void main(String[] args) { Runnable runnable = new Runnable() { @Override public void run() { while (true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } add(Thread.currentThread().getId()); } } }; Thread t1 = new Thread(runnable); Thread t2 = new Thread(runnable); Thread t3 = new Thread(runnable); Thread t4 = new Thread(runnable); t1.start(); t2.start(); t3.start(); t4.start(); } public static void add(long ID){ try{ lock.lock(); System.out.println("当前线程ID:"+ID+"执行结果="+commonTag++); }finally { lock.unlock(); } } }