线程并发及线程并发问题的解决

火车站多个窗口同时售票,假设总共500张票,四个窗口同时卖。

1.创建一个继承Thread类的子类 

package com.commons.commons.taskthread;

/**
 * @Author zhaomengxia
 * @create 2019/10/12 16:56
 */
public class SaleTicketThread extends Thread {

    static int number = 500;

    private String name;

    public SaleTicketThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (true) {
            if (number > 0) {
                System.out.println(name + "卖出" + (number--) + "号");
            } else {
                break;
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(name + "票卖完了!");
    }
}

测试 类

package com.commons.commons.taskthread;

/**
 * @Author zhaomengxia
 * @create 2019/10/12 17:02
 */
public class SaleTicketThreadTest {

    public static void main(String[] args) {

        SaleTicketThread saleTicketThread=new SaleTicketThread("窗口1");

        SaleTicketThread saleTicketThread1=new SaleTicketThread("窗口2");

        SaleTicketThread saleTicketThread2=new SaleTicketThread("窗口3");

        SaleTicketThread saleTicketThread3=new SaleTicketThread("窗口4");
        saleTicketThread.start();
        saleTicketThread1.start();
        saleTicketThread2.start();
        saleTicketThread3.start();

    }
}

从运行结果中我们可以看到有出现不同窗口卖同一张票的现象

线程并发及线程并发问题的解决_第1张图片

接下来就引出

线程并发及线程并发问题的解决_第2张图片

修改后的代码如下,就解决了上诉出现的并发问题,不会出现不同窗口卖出同一张票的情况。

第一种:

package com.commons.commons.taskthread;

/**
 * @Author zhaomengxia
 * @create 2019/10/12 16:56
 */
public class SaleTicketThread extends Thread {

    static int number = 500;

    private String name;
    
    //创建一个锁对象,这个对象是多个线程对象共享的数据
    static Object object=new Object();

    public SaleTicketThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (object){
                if (number > 0) {
                    System.out.println(name + "卖出" + (number--) + "号");
                } else {
                    break;
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(name + "票卖完了!");
    }
}

第二种

package com.commons.commons.taskthread;

/**
 * @Author zhaomengxia
 * @create 2019/10/12 16:56
 */
public class SaleTicketThread extends Thread {

    static int number = 500;

    private String name;

    //创建一个锁对象,这个对象是多个线程对象共享的数据
    static Object object = new Object();

    public SaleTicketThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (true) {
//            synchronized (object){
//                if (number > 0) {
//                    System.out.println(name + "卖出" + (number--) + "号");
//                } else {
//                    break;
//                }
//            }
            if (tickets()){
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(name + "票卖完了!");
    }


    public boolean tickets() {
        synchronized (object) {
            boolean f = false;
            if (number > 0) {
                System.out.println(name + "卖出" + (number--) + "号");
            } else {
                f = true;
            }
            return f;
        }

    }
}

第三种:这种与实现Runnable的方法不同,需要在synchronized修饰的方法上添加static,实现Runnable的方法不用

package com.commons.commons.taskthread;

/**
 * @Author zhaomengxia
 * @create 2019/10/12 16:56
 */
public  class SaleTicketThread extends Thread {

    static int number = 500;

    private String name;

    //创建一个锁对象,这个对象是多个线程对象共享的数据
    static Object object = new Object();

    public SaleTicketThread(String name) {

        super(name);
        this.name = name;
    }


    @Override
    public void run() {
        while (true) {
//            synchronized (object){
//                if (number > 0) {
//                    System.out.println(Thread.currentThread().getName() + "卖出" + (number--) + "号");
//                } else {
//                    break;
//                }
//            }
            if (tickets()){
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(name + "票卖完了!");
    }

    /**
     * 此时synchronized在类方法上,此时锁对象是类的类对象
     */
    public static synchronized boolean tickets() {
        boolean f = false;
//        synchronized (object) {
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + "卖出" + (number--) + "号");
            } else {
                f = true;
            }
//        }
        return f;

    }
}

 

你可能感兴趣的:(多线程,java,并发)