关闭

多线程计数器之CountDownLatch和join

标签: java
186人阅读 评论(0) 收藏 举报
分类:
       多线程访问全局变量,对同一全局变量的修改会影响其他线程变量的值。

       多线程访问局部变量,对变量的修改不会影响其他线程中该变量的值。


/**
 * 这个是实现计数器操作,主线程等待子线程全部完成
 * 模拟场景:10个人赛跑,当所有的人到达终点后,比赛结束
 * @author zhangm
 *
 */
public class TestCountDownLatch  {

	 //计数器,从2开始计数
	 private final static CountDownLatch mCountDownLatch = new CountDownLatch(3);
	 
	 /**
	     * 示例工作线程类
	     */
	    private static class WorkingThread extends Thread {
	        private final String mThreadName;
	        private final int mSleepTime;
	        public WorkingThread(String name, int sleepTime) {
	            mThreadName = name;
	            mSleepTime = sleepTime;
	        }
	        
	        @Override
	        public void run() {
	            System.out.println("[" + mThreadName + "] started!");
	            try {  
	                    Thread.sleep(mSleepTime);  
	            } catch (InterruptedException e) {  
	                    e.printStackTrace();  
	            }
	            mCountDownLatch.countDown();
	            System.out.println("[" + mThreadName + "] end!"); 
	        }
	    }
	    
	    /**
	     * 示例线程类
	     */
	    private static class SampleThread extends Thread {
	        
	        @Override
	        public void run() {
	            System.out.println("[SampleThread] started!");
	            try {
	                // 会阻塞在这里等待 mCountDownLatch 里的count变为0;
	                // 也就是等待另外的WorkingThread调用countDown()
	                mCountDownLatch.await();
	            } catch (InterruptedException e) {
	                
	            }
	            System.out.println("[SampleThread] end!");
	        }
	    }
	    
	    public static void main(String[] args) throws Exception {
	        // 最先run SampleThread
	        new SampleThread().start();
	        // 运行工作线程
	        new WorkingThread("WorkingThread1", 1000).start();
	        new WorkingThread("WorkingThread2", 2000).start();
	        new WorkingThread("WorkingThread3", 1000).start();
	    }
	
} 

执行结果如下:

[SampleThread] started!
[WorkingThread1] started!
[WorkingThread2] started!
[WorkingThread3] started!
[WorkingThread1] end!
[WorkingThread2] end!
[WorkingThread3] end!
[SampleThread] end!


另外还可以使用join使得主线程等待子线程完成。

public class TestJoin extends Thread{

	private  String name;
	private  int  time;
	
	public TestJoin(String name,int time)
    {
        this.name=name;
        this.time=time;
    }

	//子线程执行部分
    @Override
    public void run()
    {
        System.out.println(this.getName() + " staring...");
        try {  
            Thread.sleep(time);  
	    } catch (InterruptedException e) {  
	            e.printStackTrace();  
	    }
        System.out.println(this.getName() + " end...");
    }

    
    public static void main(String[] args)
    {
        System.out.println("main thread starting...");

        List<TestJoin> list = new ArrayList<TestJoin>();

        int time;
        for (int i = 1; i <= 5; i++)
        {
        	time=(int) (Math.random()*3+1);
        	TestJoin my = new TestJoin("Thrad " + i,1000*time);
            my.start();
            list.add(my);
        }

        try
        {
            for (TestJoin my : list)
            {
                my.join();
            }
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        System.out.println("main thread end...");

    }

}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:99440次
    • 积分:2328
    • 等级:
    • 排名:第16683名
    • 原创:130篇
    • 转载:48篇
    • 译文:0篇
    • 评论:8条
    文章分类
    最新评论