关闭

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

标签: 线程
111人阅读 评论(0) 收藏 举报
分类:

第一种:

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


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:47564次
    • 积分:1690
    • 等级:
    • 排名:千里之外
    • 原创:115篇
    • 转载:77篇
    • 译文:0篇
    • 评论:0条