A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

/**
         * 将1-10之间的数用两个线程交替输出,输出 格式为A线程打印1  B线程打印2,A线程打印3...
         */
        public static void main(String[] args) {
                final Printer p = new Printer();
               
                new Thread(){
               
                        public void run() {
                                while(true) {
                                        try {
                                                p.print1();
                                               
                                        } catch (Exception e) {
                                               
                                                e.printStackTrace();
                                        }
                                }
                        }
                }.start();
               
                new Thread(){
                       
                        public void run() {
                                while(true) {
                                        try {
                                                p.print2();
                                       
                                        } catch (Exception e) {
                                               
                                                e.printStackTrace();
                                        }
                                }
                        }
                }.start();

        }

}

class Printer {
        private int flag=1;
        private int num = 1;
       
        public void print1() throws Exception {
                while(true) {
                        synchronized (this) {
                                if(flag != 1) {
                                        this.wait();
                                }
                                if(num > 10) {
                                        break;
                                }
                                System.out.println("A线程打印" + num++);
                                flag = 2;
                                this.notify();
                        }
                }
        }
       
        public void print2() throws Exception {
                while(true) {
                        synchronized (this) {
                                if(flag != 2) {
                                        this.wait();
                                }
                                if(num > 10) {
                                        break;
                                }
                                System.out.println("B线程打印" + num++);
                                flag = 1;
                                this.notify();
                        }
                }
        }

为什么这样做出来,输出结果是正确的,但是程序没有终止,应该怎么让结果输入结束后程序终止

4 个回复

正序浏览
zhoubinjian 来自手机 金牌黑马 2016-6-26 02:29:53
报纸
一楼说的差不多了,你在判断num里面加上唤醒对方的语句就o啦
回复 使用道具 举报
这个答案找了好久,牛逼
回复 使用道具 举报
先收藏,后面慢慢看!
回复 使用道具 举报
因为你最后一个执行的线程遇到return之后就死亡了, 但是别的线程还在等待中,    处于阻塞状态,    但是并没死..
你应该在每一个线程遇到return前都把下一个预计要执行的线程唤醒,    这样程序才会终止,
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马