子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,接着再回到主线程又循环100,如此循环50次

第一种:

package thread;


public class TreadTest {

	public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        new TreadTest().init();  
  
    }  
  
    public void init(){  
        final Business business = new Business();  
        new Thread(  
                new Runnable()  
                {  
  
                    public void run() {  
                        for(int i=0;i<50;i++)  
                        {  
                            business.SubThread(i);  
                            System.out.println("循环"+i+"次");
                        }                         
                    }  
                      
                }  
          
        ).start();  
          
        for(int i=0;i<50;i++)  
        {  
            business.MainThread(i);  
        }         
    }  
      
    private class Business  {  
        boolean bShouldSub = true;//这里相当于定义了控制该谁执行的一个信号灯  
        public synchronized void MainThread(int i)  
        {  
            if(bShouldSub)  
                try {  
                    this.wait();  
                } catch (InterruptedException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }         
                  
            for(int j=0;j<5;j++)  
            {  
                System.out.println(Thread.currentThread().getName() + ":i=" + i +",j=" + j);  
            }  
            bShouldSub = true;  
            this.notify();  
          
        }  
          
          
        public synchronized void SubThread(int i)  
        {  
            if(!bShouldSub)  
                try {  
                    this.wait();  
                } catch (InterruptedException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }     
                  
            for(int j=0;j<10;j++)  
            {  
                System.out.println(Thread.currentThread().getName() + ":i=" + i +",j=" + j);  
            }  
            bShouldSub = false;               
            this.notify();            
        }  
    }  
}

第二种:

package thread;

import java.util.concurrent.Executors;  
import java.util.concurrent.ExecutorService;  
import java.util.concurrent.locks.Lock;  
import java.util.concurrent.locks.ReentrantLock;  
import java.util.concurrent.locks.Condition; 

public class CurrentThread {

	private static Lock lock = new ReentrantLock();  
    private static Condition subThreadCondition = lock.newCondition();  
    private static boolean bBhouldSubThread = false;  
    public static void main(String [] args){  
        ExecutorService threadPool = Executors.newFixedThreadPool(3);  
        threadPool.execute(new Runnable(){  
            public void run(){  
                for(int i=0;i<50;i++) {  //子线程循环次数 50
                	System.out.println("子线程循环第"+i+"次");
                    lock.lock();                      
                    try {                     
                        if(!bBhouldSubThread)  
                            subThreadCondition.await();  
                        for(int j=0;j<10;j++) {  //子线程执行次数 10
                            System.out.println(Thread.currentThread().getName() + ",j=" + j);  
                        }  
                        bBhouldSubThread = false;  
                        subThreadCondition.signal();  
                    }catch(Exception e){                         
                    }  
                    finally {  
                        lock.unlock();  
                       
                    }  
                }      
            }  
              
        });  
        threadPool.shutdown();  
        for(int i=0;i<50;i++) { //循环的次数 50 
        	System.out.println("主线程循环第"+i+"次");
                lock.lock();                      
                try {     
                    if(bBhouldSubThread)subThreadCondition.await();                               
                    for(int j=0;j<100;j++){ //主线程执行次数 100  
                        System.out.println(Thread.currentThread().getName() + ",j=" + j);  
                    }  
                    bBhouldSubThread = true;  
                    subThreadCondition.signal();                      
                }catch(Exception e){                         
                }  
                finally {  
                    lock.unlock();  
                    
                }                     
        }  
    }  
}

作者 链接:http://blog.csdn.net/nszkadrgg/article/details/8197555


阅读更多
文章标签: 线程
个人分类: 并发编程
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭