Java进阶知识查漏补缺04

Java中4种解决线程同步的方法:

1.该方法是通过实现Runnable接口创建进程 + synchronized代码块解决进程安全问题

package com.cjf.Thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: Everything
 * Date: 2020-07-01
 * Time: 18:18
 */

//               解决线程安全问题,使用线程同步机制,只要一个线程执行不完,另一个就无法执行

//    方式一:同步代码块
//      synchronized(同步监视器){
//              需要被同步的代码
//        }
//   说明: 1. 操作共享数据的代码,即为需要被同步的代码
//        2.共享数据:多个线程共同操作的变量。比如: ticket 就是共享数据。
//        3.同步监视器,俗称:锁。任何一个类的对象,都可以充当锁。
//   要求:多个线程必须要共用同一把锁。


    //该方法是通过实现Runnable接口创建进程 + synchronized代码块解决进程安全问题
class Window2 implements Runnable{

    private  int ticket=100;

    @Override
    public void run() {
        while (true){
            synchronized (this){if (ticket > 0){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ticket--;
                System.out.println(Thread.currentThread().getName()+"售出车票,余票为"+ticket);

            }else {
                break;
            }
        }}
    }
}

public class WindowTest1 {
    public static void main(String[] args) {
        Window2 window21 = new Window2();
        Thread thread1 = new Thread(window21);
        Thread thread2 = new Thread(window21);
        Thread thread3 = new Thread(window21);
        thread1.setName("一号售票口");
        thread2.setName("二号售票口");
        thread3.setName("三号售票口");
        thread1.start();
        thread2.start();
        thread3.start();


    }
}

2.该方法是通过继承Thread类创建进程 + synchronized代码块解决进程安全问题

package com.cjf.Thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: Everything
 * Date: 2020-07-01
 * Time: 18:49
 */

//该方法是通过继承Thread类创建进程 + synchronized代码块解决进程安全问题
class Window extends Thread{
    //此时必须引入静态,让三个线程实例共用一个变量
    //一提到共享问题,就要想到静态
    private static int ticket=100;
    private static Object object = new Object();

    @Override
    public void run() {
        while (true){
            synchronized (object){
            if (ticket > 0){
                try {
                    sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ticket--;
                System.out.println(Thread.currentThread().getName()+"售出车票,余票为"+ticket);

            }else {
                break;
            }
        }}
    }
}




public class WindowTest2 {
    public static void main(String[] args) {
        Window window1 = new Window();
        Window window2 = new Window();
        Window window3 = new Window();
        window1.setName("售票窗口一");
        window2.setName("售票窗口二");
        window3.setName("售票窗口三");
        window1.start();
        window2.start();
        window3.start();
    }

}

3.该方法是通过继承Thread类创建进程 + synchronized方法解决进程安全问题

package com.cjf.Thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: Everything
 * Date: 2020-07-01
 * Time: 19:32
 */


//  关于同步方法的总结:
//1.同步方法仍然涉及到同步监视器,只是不需要我们显式的声明。
//2.非静态的同步方法,同步监视器是: this
//  静态的同步方法,同步监视器是:当前类本身window2.class

//该方法是通过继承Thread类创建进程 + synchronized方法解决进程安全问题
class Window3 extends Thread{
    //此时必须引入静态,让三个线程实例共用一个变量
    //一提到共享问题,就要想到静态
    private static int ticket=100;
    private static Object object = new Object();

    @Override
    public void run() {
        while (true){
        show();

    }
}



 private static synchronized void show(){
    if (ticket > 0){
        try {
            sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ticket--;
        System.out.println(Thread.currentThread().getName()+"售出车票,余票为"+ticket);

    }
}
}






public class WindowTest3 {
    public static void main(String[] args) {
        Window3 window31 = new Window3();
        Window3 window32 = new Window3();
        Window3 window33 = new Window3();
        window31.setName("售票窗口一");
        window32.setName("售票窗口二");
        window33.setName("售票窗口三");
        window31.start();
        window32.start();
        window33.start();
    }

}

4.该方法是通过实现Runnable接口创建进程 + synchronized方法解决进程安全问题

package com.cjf.Thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: Everything
 * Date: 2020-07-01
 * Time: 19:46
 */

//该方法是通过实现Runnable接口创建进程 + synchronized方法解决进程安全问题
class Window4 implements Runnable {

    private int ticket = 100;

    @Override
    public void run() {
        while (true) {
            show();
        }

    }
    private synchronized void show(){
        if (ticket > 0){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ticket--;
            System.out.println(Thread.currentThread().getName()+"售出车票,余票为"+ticket);

        }
    }
    }


public class WindowTest4 {
    public static void main(String[] args) {
        Window4 window41 = new Window4();
        Thread thread41 = new Thread(window41);
        Thread thread42 = new Thread(window41);
        Thread thread43 = new Thread(window41);
        thread41.setName("一号售票口");
        thread42.setName("二号售票口");
        thread43.setName("三号售票口");
        thread41.start();
        thread42.start();
        thread43.start();


    }
}

 

posted @ 2020-07-01 23:53  Tsui98'  阅读(158)  评论(0编辑  收藏  举报