其他分享
首页 > 其他分享> > 多线程(下)

多线程(下)

作者:互联网

多线程(下)

线程的同步

//关于卖票解决线程安全问题(Runnable)
public class WindowTest1 {
    public static void main(String[] args) {
        Window window = new Window();
        Thread t1 = new Thread(window);
        Thread t2 = new Thread(window);
        Thread t3 = new Thread(window);
        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}
class Window implements Runnable{
    private int ticket=100;
    //创建一个对象
//    Object obj = new Object();
    @Override
    public void run() {
        while (true){
            //第一种,加入一个对象。注意:同步代码块需要使用同一个对象来加锁
//            synchronized (obj){
            //第二种,使用自身对象
            synchronized (this){
                if (ticket>0){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"卖"+ticket+"号票");
                    ticket--;
                }else{
                    break;
                }
            }
        }
    }
}
//Thread方式
public class WindowTest2 {
    public static void main(String[] args) {
        Window2 w1 = new Window2();
        Window2 w2 = new Window2();
        Window2 w3 = new Window2();
        w1.setName("线程一");
        w2.setName("线程二");
        w3.setName("线程三");
        w1.start();
        w2.start();
        w3.start();
    }
}
class Window2 extends Thread{
    public static int ticket= 100;
    public static Object obj = new Object();

    @Override
    public void run() {
            while (true){
        //加锁采用对象方式
//        synchronized (obj){
        synchronized (Window2.class){//Window2.class只会加载一次

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (ticket>0){
                    System.out.println(Thread.currentThread().getName()+"卖第"+ticket+"张票");
                    ticket--;
                }else{
                    break;
                }
        }

        }
    }
}
//使用同步方法解决实现Runnable接口的线程安全问题
public class WindowTest3 {
    public static void main(String[] args) {
        Window3 w = new Window3();
        Thread t1 = new Thread(w);
        Thread t2 = new Thread(w);
        Thread t3 = new Thread(w);
        t1.start();
        t2.start();
        t3.start();
    }
}
class Window3 implements Runnable{
    private int ticket = 100;

    @Override
    public void run() {
        while (ticket>0){
            show();
        }
    }
    private synchronized void show(){//同步监视器---this
        //相当于synchronized(this){
        if (ticket>0){
            System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票");
            ticket--;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
//使用同步方法处理继承Thread类的方式中的线程安全问题
public class WindowTest4 {
    public static void main(String[] args) {
        Window4 w1 = new Window4();
        Window4 w2 = new Window4();
        Window4 w3 = new Window4();
        w1.setName("窗口一");
        w2.setName("窗口二");
        w3.setName("窗口三");
        w1.start();
        w2.start();
        w3.start();
    }
}
class Window4 extends Thread{
    private static int ticket = 1000;

    @Override
    public void run() {
        while (ticket>0){
            show();
        }
    }
    private static synchronized void show(){//同步监视器:Window4.class
        if (ticket>0){
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"票");
            ticket--;
        }
    }
}

标签:同步,Thread,线程,new,ticket,多线程,public
来源: https://www.cnblogs.com/rhy2103/p/16271686.html