【JAVA基础】- 多线程电影票售票案例

三种实现方式

继承Thread类,synchronized同步代码块

public class SellTicket extends Thread {

    // 多个对象共享,必须static修饰
    private static int tickets = 100;

    @Override
    public void run() {
        while (true) {
            synchronized (SellTicket.class) {
                try {
                    sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (tickets > 0) {
                    System.out.println(getName() + " 正在出售第 " + tickets-- + " 张票");
                }
            }
        }
    }
}

public class TicketDemo {

    public static void main(String[] args){
        SellTicket thread01 = new SellTicket();
        SellTicket thread02 = new SellTicket();
        SellTicket thread03 = new SellTicket();

        thread01.setName("窗口一");
        thread02.setName("窗口二");
        thread03.setName("窗口三");

        thread01.start();
        thread02.start();
        thread03.start();
    }
}

实现runnable接口,同步代码块

public class SellTicket2 implements Runnable {

    // 多个对象共享,必须static修饰
    private static int tickets = 100;

    @Override
    public void run() {
        while (true) {
            synchronized (SellTicket2.class) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " 正在出售第 " + tickets-- + " 张票");
                }
            }
        }
    }
}

public class TicketDemo2 {

    public static void main(String[] args) {
        SellTicket2 sellTicket2 = new SellTicket2();

        Thread thread01 = new Thread(sellTicket2);
        Thread thread02 = new Thread(sellTicket2);
        Thread thread03 = new Thread(sellTicket2);

        thread01.setName("窗口一");
        thread02.setName("窗口二");
        thread03.setName("窗口三");

        thread01.start();
        thread02.start();
        thread03.start();
    }
}

实现runable接口,Lock锁实现线程安全

public class SellTicket3 implements Runnable {

    // 多个对象共享,必须static修饰
    private static int tickets = 100;

    private Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock.lock();
            if (tickets > 0) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " 正在出售第 " + tickets-- + " 张票");
            }
            lock.unlock();
        }
    }
}

public class TicketDemo3 {

    public static void main(String[] args) {
        SellTicket3 sellTicket3 = new SellTicket3();

        Thread thread01 = new Thread(sellTicket3);
        Thread thread02 = new Thread(sellTicket3);
        Thread thread03 = new Thread(sellTicket3);

        thread01.setName("窗口一");
        thread02.setName("窗口二");
        thread03.setName("窗口三");

        thread01.start();
        thread02.start();
        thread03.start();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 进程和线程 进程 所有运行中的任务通常对应一个进程,当一个程序进入内存运行时,即变成一个进程.进程是处于运行过程中...
    胜浩_ae28阅读 5,254评论 0 23
  • Java多线程学习 [-] 一扩展javalangThread类 二实现javalangRunnable接口 三T...
    影驰阅读 3,097评论 1 18
  • 该文章转自:http://blog.csdn.net/evankaka/article/details/44153...
    加来依蓝阅读 7,462评论 3 87
  • 本文主要讲了java中多线程的使用方法、线程同步、线程数据传递、线程状态及相应的一些线程函数用法、概述等。 首先讲...
    李欣阳阅读 2,575评论 1 15
  • 写在前面的话: 这篇博客是我从这里“转载”的,为什么转载两个字加“”呢?因为这绝不是简单的复制粘贴,我花了五六个小...
    SmartSean阅读 4,921评论 12 45

友情链接更多精彩内容