模拟叫号看病 特需号和普通号

编写

package com.Input_Output.I_O2;
/**
 *     普通号病人
 * @author Administrator
 *    多线程
 */
public class HospitalTest {
    public static void main(String[] args) {

        //创建对象
        Thread h=Thread.currentThread();    //创建普通号病人 获取当前进程
        vip v=new vip();                    //创建特需号病人

        //设置进程名
        h.setName("普通号");
        v.setName("特需号");

        //run方法
        v.start();                    //特需号
        
        for(int i=1;i<=30;i++) {    //普通号
            System.out.println("●"+h.getName()+":"+i+"号病人正在看病");
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //当普通号等于10
            if(i==10) {
                try {
                    v.join();        //当普通号等于10,等待特需号终止,再执行
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

/**
 *     特需号病人
 */
class vip extends Thread{

    @Override
    public void run() {
        String str=Thread.currentThread().getName();    //获取当前线程名

        for(int i=1;i<=10;i++) {
            System.out.println("○"+str+":"+i+"号病人正在看病");
            try {
                Thread.sleep(600);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行

猜你喜欢

转载自www.cnblogs.com/zzh630/p/10500777.html