线程的同步与死锁

2020-12-01  本文已影响0人  曾梦想仗剑天涯

同步问题的引出

//
package com.company;
class MyThread implements Runnable {
    private int ticket = 10;
    @Override
    public void run() {
        while (true) {
            if (this.ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "卖出去一张票,当前还剩" + this.ticket--);
            } else {
                System.out.println("******* 票卖完了 *******");
                break;
            }
        }
    }
}
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        MyThread mt = new MyThread();
        Thread threadA = new Thread(mt, "票贩子A");
        Thread threadB = new Thread(mt, "票贩子B");
        Thread threadC = new Thread(mt, "票贩子C");
        threadA.start();
        threadB.start();
        threadC.start();
    }
}
if (this.ticket > 0) {
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
         e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + "卖出去一张票,当前还剩" + this.ticket--);
} else {
    System.out.println("******* 票卖完了 *******");
    break;
}
此图来源于李兴华老师

线程同步处理

此图来源于李兴华老师
//利用同步代码块解决数据同步访问问题
synchronized (this) {
    if (this.ticket > 0) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "卖出去一张票,当前还剩" + this.ticket--);
    } else {
        System.out.println("******* 票卖完了 *******");
        break;
    }
}
//利用同步方法解决:只需要在方法定义上使用synchronized关键字即可
package com.company;
class MyThread implements Runnable {
    private int ticket = 10;
    public synchronized boolean sale() {
        if (this.ticket > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "卖出去一张票,当前还剩" + this.ticket--);
            return true;
        } else {
            System.out.println("******* 票卖完了 *******");
            return false;
        }
    }
    @Override
    public void run() {
        while (this.sale()) {
            ;
        }
    }
}
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        MyThread mt = new MyThread();
        Thread threadA = new Thread(mt, "票贩子A");
        Thread threadB = new Thread(mt, "票贩子B");
        Thread threadC = new Thread(mt, "票贩子C");
        threadA.start();
        threadB.start();
        threadC.start();
    }
}

线程的死锁

//死锁的展示
package com.company;
public class DeadLock implements Runnable {
    private LockA a = new LockA();
    private LockB b = new LockB();
    @Override
    public void run() {
        a.say(b);
    }
    public DeadLock() {
        new Thread(this).start();
        b.say(a);
    }
    public static void main(String[] args) {
        new DeadLock();
    }
}
class LockA {
    public synchronized void say(LockB b) {
        System.out.println("线程A说:线程B你让我先运行");
        b.get();
    }
    public synchronized void get() {
        System.out.println("线程B说了让我先运行");
    }
}
class LockB {
    public synchronized void say(LockA a) {
        System.out.println("线程B说:线程A你让我先运行");
        a.get();
    }
    public synchronized void get() {
        System.out.println("线程A说了让我先运行");
    }
}
上一篇下一篇

猜你喜欢

热点阅读