Chinaunix首页 | 论坛 | 博客
  • 博客访问: 104523
  • 博文数量: 41
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 352
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-23 12:37
文章分类

全部博文(41)

文章存档

2015年(1)

2014年(28)

2013年(12)

我的朋友

分类: Java

2014-08-30 20:40:23

子线程循环10次,接着主线程循环10,接着又回到子线程循环10次,接着再回到主线程又循环10,如此循环50次,请写出程序。
方法1:
    

点击(此处)折叠或打开

  1. package com.wp;

  2. import java.util.concurrent.Executors;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.locks.Lock;
  5. import java.util.concurrent.locks.ReentrantLock;
  6. import java.util.concurrent.locks.Condition;

  7. public class ThreadTest2{
  8. private static Lock lock = new ReentrantLock();
  9. private static Condition subThreadCondition = lock.newCondition();
  10. private static boolean bBhouldSubThread = false;

  11. public static void main(String [] args)
  12. {
  13.    ExecutorService threadPool = Executors.newFixedThreadPool(3);

  14.    threadPool.execute(new Runnable(){
  15.      public void run(){
  16.           for(int i=0;i<50;i++){              
  17.               lock.lock();
  18.               try{
  19.                   if(!bBhouldSubThread)
  20.                       subThreadCondition.await();
  21.                    for(int j=0;j<10;j++){
  22.                        System.out.println(Thread.currentThread().getName()+ ",j=" + j);
  23.                    }
  24.                    bBhouldSubThread= false;
  25.                    subThreadCondition.signal();
  26.               }catch(Exception e){
  27.               }finally{
  28.                   lock.unlock();
  29.               }
  30.           }
  31.      }
  32.                        });
  33.    
  34.                    threadPool.shutdown();
  35.                    for(int i=0;i<50;i++){
  36.                        lock.lock();
  37.                        try{
  38.                            if(bBhouldSubThread)
  39.                                subThreadCondition.await();
  40.                            for(int j=0;j<10;j++){
  41.                                System.out.println(Thread.currentThread().getName()+ ",j=" + j);
  42.                            }
  43.                            bBhouldSubThread= true;
  44.                            subThreadCondition.signal();
  45.                        }catch(Exception e){
  46.                        }finally{
  47.                            lock.unlock();
  48.                        }
  49.                    }
  50.     }
  51. }
方法2:
  

点击(此处)折叠或打开

  1. package com.wp;
  2. public class ThreadTest3 {
  3.     private static boolean bShouldMain=false;
  4.     public static void main(String[]args) {
  5.     // TODO Auto-generated method stub

  6.     new Thread(new Runnable(){
  7.                                 public void run(){
  8.                                     for(int i=0;i<50;i++){
  9.                                         
  10.                                         synchronized(ThreadTest.class){
  11.                                             if(bShouldMain){
  12.                                                 try{
  13.                                                      ThreadTest.class.wait();
  14.                                                     
  15.                                                     }catch(InterruptedException e){
  16.                                                     e.printStackTrace();
  17.                                                 }
  18.                                             }
  19.                                             
  20.                                             for(int j=0;j<10;j++){
  21.                                                 System.out.println(Thread.currentThread().getName()+"i="+ i + ",j=" + j);
  22.                                             }
  23.                                             
  24.                                             bShouldMain= true;
  25.                                             ThreadTest.class.notify();
  26.                                         }
  27.                                         
  28.                                     }
  29.                                 }
  30.                         }
  31.                 ).start();
  32.     
  33.     for(int i=0;i<50;i++){
  34.         
  35.      synchronized (ThreadTest.class){
  36.           if(!bShouldMain){
  37.               try{
  38.                   ThreadTest.class.wait();
  39.               }catch(InterruptedException e){
  40.                   e.printStackTrace();
  41.               }
  42.           }
  43.          
  44.      for(int j=0;j<10;j++){
  45.          System.out.println(Thread.currentThread().getName()+"i=" + i +",j=" + j);
  46.      }
  47.          bShouldMain=false;
  48.          ThreadTest.class.notify();
  49.      }
  50.      }
  51.      }
  52.     }


   


阅读(521) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~