四窗口卖票 实现Runnable

来源:互联网 发布:程序员的线性代数 知乎 编辑:程序博客网 时间:2024/04/30 02:01
package com.heima.lei;public class Test7 {    /**四窗口卖票  实现Runnable     * @param args     */    public static void main(String[] args) {        MyRunnable1 mt = new MyRunnable1();        Thread t1 = new Thread(mt);        Thread t2 = new Thread(mt);        Thread t3 = new Thread(mt);        Thread t4 = new Thread(mt);        t1.setName("第一窗口:");        t2.setName("第二窗口:");        t3.setName("第三窗口:");        t4.setName("第四窗口:");        t1.start();        t2.start();        t3.start();        t4.start();    }}class MyRunnable1 implements Runnable{    private  int ticket = 100;    @Override    public void run() {        while(true){            synchronized (MyRunnable1.class) {                if(ticket == 0){                    break;                }                try {                    Thread.sleep(10);                } catch (InterruptedException e) {                    e.printStackTrace();                }                System.out.println(Thread.currentThread().getName() + "这是第" + ticket-- +"号票!");            }        }    }}
0 0
原创粉丝点击