# 死锁和活锁

// 死锁
class Account {
    int balance = 100;

    void transfer(Account target, int amount) {
        synchronized (this) {
            synchronized (target) {
                if (amount <= balance) {
                    this.balance -= amount;
                    target.balance += amount;
                }
            }
        }
    }
}

// 活锁
class Account {
    int balance = 100;
    private Lock lock = new ReentrantLock();
    
    void transfer(Account target, int amount) {
        while (true) {
            if (this.lock.tryLock()) {
               try {
                   if (target.lock.tryLock()) {
                       try {
                           this.balance -= amount;
                           target.balance += amount;
                       } finally {
                           target.lock.unlock();
                       }
                   }
               } finally {
                   this.lock.unlock();
               }
            }
        }
    }
}
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

# 2 个线程轮流执行

// 1. 利用 synchronized,线程会阻塞
class Print implements Runnable {
    volatile int counter = 0;

    @Override
    public void run() {
        synchronized (this) {
            while (counter <= 100) {
                System.out.println(Thread.currentThread().getName() + ": " + counter);
                counter++;
                this.notify();
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class TwoThreadPrint {
    public static void main(String[] args) {
        Print print = new Print();
        new Thread(print).start();
        new Thread(print).start();
    }
}
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

# 3 个线程轮流执行

// 1. 利用 volatile
class Print {
    volatile int counter = 0;
    volatile int flag = 0;

    public void test() {
        new Thread(() -> {
            while (counter <= 100) {
                if (flag == 0) {
                    System.out.println(Thread.currentThread().getName() + ": " + counter);
                    counter++;
                    flag = 1;
                }
            }
        }).start();

        new Thread(() -> {
            while (counter <= 100) {
                if (flag == 1) {
                    System.out.println(Thread.currentThread().getName() + ": " + counter);
                    counter++;
                    flag = 2;
                }
            }
        }).start();

        new Thread(() -> {
            while (counter <= 100) {
                if (flag == 2) {
                    System.out.println(Thread.currentThread().getName() + ": " + counter);
                    counter++;
                    flag = 0;
                }
            }
        }).start();
    }
}

// 2. 利用 Condition
// 注意:需要先 signal 再 await,从第一轮开始分析即可
class Print {
    volatile int counter = 0;

    final Lock lock = new ReentrantLock(true);
    final Condition c1 = lock.newCondition();
    final Condition c2 = lock.newCondition();
    final Condition c3 = lock.newCondition();

    public void test() {
        new Thread(() -> {
            while (counter <= 100) {
                lock.lock();
                try {
                    System.out.println(Thread.currentThread().getName() + ": " + counter);
                    counter++;
                    c2.signal();
                    c1.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }).start();

        new Thread(() -> {
            while (counter <= 100) {
                lock.lock();
                try {
                    System.out.println(Thread.currentThread().getName() + ": " + counter);
                    counter++;
                    c3.signal();
                    c2.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }).start();

        new Thread(() -> {
            while (counter <= 100) {
                lock.lock();
                try {
                    System.out.println(Thread.currentThread().getName() + ": " + counter);
                    counter++;
                    c1.signal();
                    c3.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }).start();
    }
}
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

# Interrupt 使用

public static void main(String[] args) throws InterruptedException {
    // 主动检测
    Thread t1 = new Thread(() -> {
        System.out.println("t1 start");
        while (!Thread.currentThread().isInterrupted()) {
            // do some
        }
        System.out.println("t1 done");
    });

    // 被动通知
    Thread t2 = new Thread(() -> {
        while (!Thread.currentThread().isInterrupted()) {
            System.out.println("t2 start");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // 抛出异常后,中断标示会自动清除掉,因此需要自己中断自己一次
                Thread.currentThread().interrupt();  
            }
        }
        System.out.println("t2 done");
    });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Last Updated: 9/18/2020, 6:40:58 AM