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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈永波 初级黑马   /  2012-7-2 14:07  /  1433 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 陈永波 于 2012-7-3 14:57 编辑

public class Exercise3 {
        public static void main(String[] args) {
                final Printer p=new Printer();
                new Thread(){
                        public void run()
                        {
                                for(int i=0;i<5;i++)
                                        p.print1();
                                
                        }
                        
                }.start();
                new Thread(){
                        public void run()
                        {
                                for(int i=0;i<5;i++)
                                        p.print2();
                                
                        }
                        
                }.start();

        }

}
class Printer
{
        private int turn=1;
        public synchronized void print1()
        {
                if(turn!=1)
                        try {
                                this.wait();
                        } catch (InterruptedException e) {
                                
                                e.printStackTrace();
                        }
                else
                        System.out.println("A");
                turn=2;
                this.notify();
        }
        public synchronized void print2()
        {
                if(turn!=2)
                        try {
                                this.wait();
                        } catch (InterruptedException e) {
                                
                                e.printStackTrace();
                        }
                else
                    System.out.println("B");
                turn=1;
                this.notify();
        }
}
这里为什么只打印了一次A,B?那里出了错误?

2 个回复

倒序浏览
把else去掉,这个法最好是为while这样更稳妥一些
回复 使用道具 举报
public class Exercise3 {
        public static void main(String[] args) {
                final Printer p=new Printer();
                new Thread(){
                        public void run()
                        {
                                for(int i=0;i<5;i++)
                                        p.print1();//假设这个线程是A线程                                
                        }
                        
                }.start();
                new Thread(){
                        public void run()
                        {
                                for(int i=0;i<5;i++)
                                        p.print2();//假设这个线程是B线程                                
                        }
                        
                }.start();

        }

}
class Printer//加上黑体字的代码就可以看到两个线程工作的全过程了
{
        private int turn=1;
        public synchronized void print1()
        {
                if(turn!=1)
                        try {
                                  System.out.println("A等待");   
                             this.wait();
                        } catch (InterruptedException e) {
                                
                                e.printStackTrace();
                        }
                else//可以把这个else去掉
                        System.out.println("A");
               System.out.println("A里面的turn要改变成2了");  
              turn=2;
              System.out.println("A里面的turn 已经改变成2了");   
             this.notify();
        }
        public synchronized void print2()
        {
                if(turn!=2)
                        try {
                              System.out.println("B等待");      
                         this.wait();
                        } catch (InterruptedException e) {
                                
                                e.printStackTrace();
                        }
                else//可以去掉这个else     
               System.out.println("B");
            System.out.println("B里面的turn要改变成1了");            
                turn=1;
          System.out.println("B里面的turn 已经改变成1了");     
           this.notify();
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马