java 多线程买票案例

package com.tedu.threadStudy;

public class studyTicket {
    public static void main(String[] args) {
        YouThread youThread = new YouThread();
        Thread thread = new Thread(youThread,"窗口1");
        Thread thread2 = new Thread(youThread,"窗口2");
        Thread thread3 = new Thread(youThread,"窗口3");
        thread.start();
        thread2.start();
        thread3.start();
    }
}

class YouThread implements Runnable{
    int ticket = 100;

    @Override
    public void run() {
        while (ticket>0){
            System.out.println("正在售卖第"+(ticket--)+"张票");

        }
//        while (true){
//            if (ticket>0){
//                System.out.println(Thread.currentThread().getName()+"...卖出第"+(ticket--)+"张票");
//            }else {
//                break;
//            }
//        }
    }
}
posted @ 2022-11-07 23:18  竹石2020  阅读(59)  评论(0编辑  收藏  举报