一、使用关键字:synchronized
1、synchronized可以修饰方法,(其实也是锁的是当前方法的对象),也可以修饰代码块,修饰代码也需要传入一个对象。 synchronized(object){}
2、两种方式都是获取的是synchronized修饰对象的monitor锁!这才是真正的锁!
3、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
如果不用则结果如下:
当前线程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
二、死锁的原因和如何避免!
对于程序员需要关注两个方向:一个是内存不足引起的死锁,另一个是交叉锁,各持对方的排它锁导致的死锁,谁都不肯放!
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++);
}
}
}
}
三、LOCK(java.util.concurrent.locks)
1、读写锁?
2、自旋锁
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();
}
}
}