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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lxww 中级黑马   /  2013-3-19 19:38  /  1363 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 kingdom_202 于 2013-3-19 21:26 编辑

class Demo2_Exercise1 {

public static void main(String[] args) {
  final Printer p1 = new Printer();
  final Printer p2 = new Printer();
  
  new Thread("线程1:"){
   public void run() {
    for (int i = 1; i <= 3; i++)
     try{
      p1.print();
     }catch(Exception e){
      e.printStackTrace();
     }
   }
  }.start();
  
  new Thread("线程2:"){
   public void run() {
    for (int i = 1; i <= 3; i++)
     try{
      p2.print();
     }catch(Exception e){
      e.printStackTrace();
     }
   }
  }.start();
}
  
}
class Printer{
public synchronized void print() throws Exception {
  for (int i = 1; i <= 3; i++)
  System.out.println(Thread.currentThread().getName()+ i);
  System.out.println();
}
}

以上代码怎么实现以下运行效果:不能使随机得到的,要确保每次都是得到这个结果。请求高手指点!(重点:在主函数内实现)
线程1: 1
线程1: 2
线程1: 3

线程2: 1
线程2: 2
线程2: 3

线程1: 1
线程1: 2
线程1: 3

线程2: 1
线程2: 2
线程2: 3
  
线程1: 1
线程1: 2
线程1: 3

线程2: 1
线程2: 2
线程2: 3

评分

参与人数 1技术分 +1 收起 理由
猫腻 + 1

查看全部评分

3 个回复

正序浏览
第一种:synchronized
class Printer {
        boolean flag=false;
        public  synchronized void print1() {
                while(flag)
                        try{wait();}catch(Exception e){}
                for (int i = 1; i <= 3; i++)
                        System.out.println("线程1: " + i);
                System.out.println();
                flag=true;
                notifyAll();
        }
       
        public  synchronized void print2() {
                while(!flag)
                try{wait();}catch(Exception e){}
                for (int i = 1; i <= 3; i++)
                        System.out.println("线程2: " + i);
                System.out.println();
                flag=false;
                notifyAll();
        }       
}

第二种:  JDK1.5后,用lock锁

class Print

{        private ReentrantLock lock=new ReentrantLock();
        private Condition con_1=lock.newCondition();
        private Condition con_2=lock.newCondition();
       
        int flag=1;
        public  void print1() throws InterruptedException {
                lock.lock();
                try{while(flag!=1)
                        con_1.await();
                for(int x=1;x<=3;x++){
                        System.out.println("线程一:"+x);
                        System.out.println();
                }
                flag=2;
                con_2.signal();}
                finally{lock.unlock();}
        }
        public  void print2() throws InterruptedException {
                lock.lock();
                while(flag!=2)
                        con_2.await();
                for(int x=1;x<=3;x++){
                        System.out.println("线程二:"+x);
                        System.out.println();
                }
                flag=1;
                con_1.signal();
                lock.unlock();
        }

评分

参与人数 1技术分 +1 收起 理由
猫腻 + 1

查看全部评分

回复 使用道具 举报
  1. class Demo2_Exercise {
  2.        
  3.         public static void main(String[] args) {
  4.                 final Printer p = new Printer();
  5.                
  6.                 new Thread(){
  7.                         public void run() {
  8.                                 for (int i = 1; i <= 3; i++)
  9.                                         try {
  10.                                                 p.print1();
  11.                                         } catch(Exception e) {
  12.                                                 e.printStackTrace();       
  13.                                         }
  14.                         }       
  15.                 }.start();
  16.                
  17.                 new Thread(){
  18.                         public void run() {
  19.                                 for (int i = 1; i <= 3; i++)
  20.                                         try {
  21.                                                 p.print2();
  22.                                         } catch(Exception e) {
  23.                                                 e.printStackTrace();       
  24.                                         }
  25.                         }       
  26.                 }.start();
  27.         }
  28.                
  29. }

  30. class Printer {
  31.         private int flag = 1;
  32.        
  33.         public synchronized void print1() throws Exception {
  34.                 if (flag != 1)
  35.                         wait();
  36.                 for (int i = 1; i <= 3; i++)
  37.                         System.out.println("线程1: " + i);
  38.                 System.out.println();
  39.                 flag = 2;
  40.                 notify();
  41.         }
  42.        
  43.         public synchronized void print2() throws Exception {
  44.                 if (flag != 2)
  45.                         wait();
  46.                 for (int i = 1; i <= 3; i++)
  47.                         System.out.println("线程2: " + i);
  48.                 System.out.println();
  49.                 flag = 1;
  50.                 notify();
  51.         }       
  52. }
复制代码
线程等待, 就调用wait()

notify()方法是随机唤醒一个线程

评分

参与人数 1技术分 +1 收起 理由
猫腻 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 张宝 于 2013-3-19 19:47 编辑

/*
        线程1: 1
        线程1: 2
        线程1: 3
        
        线程2: 1
        线程2: 2
        线程2: 3
        
        
        
        线程1: 1
        线程1: 2
        线程1: 3
        
        线程2: 1
        线程2: 2
        线程2: 3
        
        
        
        线程1: 1
        线程1: 2
        线程1: 3
        
        线程2: 1
        线程2: 2
        线程2: 3
        
        
        
*/
class Demo2_Exercise 1{
        
        public static void main(String[] args) {
                final Printer p = new Printer();
               
                new Thread(){
                        public void run() {
                                for (int i = 1; i <= 3; i++)
                                        try {
                                                p.print1();
                                        } catch(Exception e) {
                                                e.printStackTrace();        
                                        }
                        }        
                }.start();
               
                new Thread(){
                        public void run() {
                                for (int i = 1; i <= 3; i++)
                                        try {
                                                p.print2();
                                        } catch(Exception e) {
                                                e.printStackTrace();        
                                        }
                        }        
                }.start();
        }
               
}

class Printer {
        private int flag = 1;
        
        public synchronized void print1() throws Exception {
                if (flag != 1)
                        wait();
                for (int i = 1; i <= 3; i++)
                        System.out.println("线程1: " + i);
                System.out.println();
                flag = 2;
                notify();
        }
        
        public synchronized void print2() throws Exception {
                if (flag != 2)
                        wait();
                for (int i = 1; i <= 3; i++)
                        System.out.println("线程2: " + i);
                System.out.println();
                flag = 1;
                notify();
        }        
}

评分

参与人数 1技术分 +1 收起 理由
猫腻 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马