码迷,mamicode.com
首页 > 其他好文 > 详细

练习四

时间:2019-08-06 15:39:46      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:run   pre   stack   inter   system   png   i++   current   for   

技术图片

 

package zuoYe03;

public class Sickness {

    public static void main(String[] args) {

        // 特需房
        Thread t2 = new Thread("特需号") {

            public void run() {
                int i;
                for (i = 1; i <= 10; i++) {
                    System.out.println(Thread.currentThread().getName() + " : " + i + "号病人在看病!");

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                if (i > 10) {
                    System.out.println("特需房一天看病次数已满,不能再看病!");
                }

            }
        };
        // 普通号
        Thread t1 = new Thread("普通号") {

            public void run() {

                int i;
                for (i = 1; i <= 50; i++) {
                    System.out.println(Thread.currentThread().getName() + " : " + i + "号病人在看病!");

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    if (i == 10) {

                        try {
                            t2.join();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }

                if (i > 50) {
                    System.out.println("普通房一天看病次数已满,不能再看病!");
                }
            }

        };

        t1.start();
        t2.start();
    }

}

技术图片

 

练习四

标签:run   pre   stack   inter   system   png   i++   current   for   

原文地址:https://www.cnblogs.com/bichen-01/p/11309115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!