编程语言
首页 > 编程语言> > Java多线程之八锁问题

Java多线程之八锁问题

作者:互联网

1 八锁现象

public class Lock_8_1 {
    public static void main(String[] args) throws Exception {
        Phone phone = new Phone();
        new Thread(() -> { phone.sendSMS(); }, "AA").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(() -> { phone.sendEmail(); }, "BB").start();
    }
}

class Phone {
    public synchronized void sendSMS() {
        System.out.println("------sendSMS");
    }
    public synchronized void sendEmail() {
        System.out.println("------sendEmail");
    }
    public void getHello() {
        System.out.println("------getHello");
    }
}/*sendSMS*/
public class Lock_8_2 {
    public static void main(String[] args) throws Exception {
        Phone phone = new Phone();
        new Thread(() -> { phone.sendSMS(); }, "AA").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(() -> { phone.sendEmail(); }, "BB").start();
    }
}

class Phone {
    public synchronized void sendSMS() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("------sendSMS");
    }
    public synchronized void sendEmail() {
        System.out.println("------sendEmail");
    }
}/*sendSMS*/
public class Lock_8_3 {
    public static void main(String[] args) throws Exception {
        Phone phone = new Phone();
        new Thread(() -> { phone.sendSMS(); }, "AA").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(() -> { phone.getHello(); }, "BB").start();
    }
}

class Phone {
    public synchronized void sendSMS() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("------sendSMS");
    }
    public synchronized void sendEmail() {
        System.out.println("------sendEmail");
    }
    public void getHello() {
        System.out.println("------getHello");
    }
}/*getHello*/
public class Lock_8_4 {
    public static void main(String[] args) throws Exception {
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(() -> { phone1.sendSMS(); }, "AA").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(() -> { phone2.sendEmail(); }, "BB").start();
    }
}

class Phone {
    public synchronized void sendSMS() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("------sendSMS");
    }
    public synchronized void sendEmail() {
        System.out.println("------sendEmail");
    }
}/*sendEmail*/
public class Lock_8_5 {
    public static void main(String[] args) throws Exception {
        Phone phone = new Phone();
        new Thread(() -> { phone.sendSMS(); }, "AA").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(() -> { phone.sendEmail(); }, "BB").start();
    }
}

class Phone {
    public static synchronized void sendSMS() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("------sendSMS");
    }
    public static synchronized void sendEmail() {
        System.out.println("------sendEmail");
    }
}/*sendSMS*/
public class Lock_8_6 {
    public static void main(String[] args) throws Exception {
        Phone phone = new Phone();
        Phone phone2 = new Phone();
        new Thread(() -> { phone.sendSMS(); }, "AA").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(() -> { phone2.sendEmail(); }, "BB").start();
    }
}

class Phone {
    public static synchronized void sendSMS() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("------sendSMS");
    }
    public static synchronized void sendEmail() {
        System.out.println("------sendEmail");
    }
}/*sendSMS*/
public class Lock_8_7 {
    public static void main(String[] args) throws Exception {
        Phone phone = new Phone();
        new Thread(() -> { phone.sendSMS(); }, "AA").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(() -> { phone.getHello(); }, "BB").start();
    }
}

class Phone {
    public static synchronized void sendSMS() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("------sendSMS");
    }
    public synchronized void sendEmail() {
        System.out.println("------sendEmail");
    }
}/*sendEmail*/
public class Lock_8_8 {
    public static void main(String[] args) throws Exception {
        Phone phone = new Phone();
        Phone phone2 = new Phone();
        new Thread(() -> { phone.sendSMS(); }, "AA").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(() -> { phone2.sendEmail(); }, "BB").start();
    }
}

class Phone {
    public static synchronized void sendSMS() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("------sendSMS");
    }
    public synchronized void sendEmail() {
        System.out.println("------sendEmail");
    }
}/*sendEmail*/

2 多线程锁

标签:Java,sendEmail,sendSMS,Phone,void,new,多线程,之八锁,public
来源: https://www.cnblogs.com/wuweibincqu/p/14138637.html