写在前边,
/**
* 线程互斥同步:通过两种锁机制来控制多个线程对共享资源的互斥访问,synchronized、ReentrantLock
*/

一、JVM实现的synchronized。使用synchronized不用担心没有释放锁导致死锁的问题,因为jvm会确认锁的释放
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class SynchronizedExample{
 
    public synchronized void m1_1(){
        for (int i =0;i<10;i++){
            System.out.println(i+"");
        }
    }
 
    public synchronized void m2_1(){
        for (int i =11;i<20;i++){
            System.out.println(i+"");
        }
    }
 
 
    public synchronized static void m1_1_1(){
        // 同步一个静态方法
    }
 
    public void m1_2(){
        // 同步一个类,作用于整个类,如果两个线程调用同一个类的不同对象,也会进行同步
        synchronized (SynchronizedExample.class){
            for (int i=0;i<10;i++){
                System.out.println(i+"");
            }
        }
    }
 
    public void m2(){
        // 同步代码块
        synchronized (this){
            for (int i=0;i<10;i++){
                System.out.println(i+"");
            }
        }
    }
 
    public static void main(String[] args) {
        SynchronizedExample se1 = new SynchronizedExample();
        SynchronizedExample se2 = new SynchronizedExample();
        ExecutorService executorService = Executors.newCachedThreadPool();
        // 以下两行,两个线程调用了同一对象的同步代码块,因此这两个线程会进行同步。当一个线程进入同步语句块时,另一个线程必须等待
//        executorService.execute(se1::m2);
//        executorService.execute(se1::m2);
 
        // 以下两行,两个线程调用了同一对象的两个同步方法,这两个线程也会同步
//        executorService.execute(se1::m1_1);
//        executorService.execute(se1::m2_1);
 
        // 以下两行,两个线程调用了不同对象的同步代码块,因此这两个线程不需要同步,两个线程交叉执行
//        executorService.execute(se1::m2);
//        executorService.execute(se2::m2);
 
        // 以下两行,两个线程调用了不同对象的作用于整个类的同步代码块,因此这两个线程会进行同步。当一个线程进入同步语句块时,另一个线程必须等待
//        executorService.execute(se1::m1_2);
//        executorService.execute(se2::m1_2);
 
        executorService.shutdownNow();
    }
 
}

二、jdk实现的ReentrantLock,是J.U.C包中的锁
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class ReentrantLockExample{
 
    private Lock lock = new ReentrantLock();
 
    public void m1(){
        lock.lock();
        try{
            for (int i =0;i<10;i++){
                System.out.println(i+"");
            }
        }finally {
            lock.unlock();  // 确保释放锁,避免死锁
        }
    }
 
    public static void main(String[] args) {
        ReentrantLockExample reentrantLockExample = new ReentrantLockExample();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(reentrantLockExample::m1);
        executorService.execute(reentrantLockExample::m1);
 
        executorService.shutdown();
 
    }
 
}
>>>输出结果
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9