• JAVA多线程两个实用的辅助类(CountDownLatch和AtomicBoolean)


    AtomicBoolean它允许一个线程等待一个线程完成任务,然后运行:

    A boolean value that may be updated atomically. See the java.util.concurrent.atomic package specification for description of the properties of atomic variables. An AtomicBoolean is used in applications such as atomically updated flags, and cannot be used as a replacement for a Boolean.

    public static void main(String[] args) {
    		Thread t2 = new Thread(new BarWorker("bb"));
    		Thread t1 = new Thread(new BarWorker("aa"));
    		t2.run();
    		t1.run();
    	}
    
    	
    	private static class BarWorker implements Runnable {
    
    		private static AtomicBoolean exists = new AtomicBoolean(false);
    
    		private String name;
    
    		public BarWorker(String name) {
    			this.name = name;
    		}
    
    		public void run() {
    			if (exists.compareAndSet(false, true)) {  //当第一个线程设置为true后,另外的线程是进不来的
    				
    				System.out.println(name + " enter"+"currentvalue="+exists.get());
    				try {
    					System.out.println(name + " working");
    					Thread.sleep(2000);
    				} catch (InterruptedException e) {
    					// do nothing
    				}
    				System.out.println(name + " leave");
    				exists.set(false);
    			} else {
    				System.out.println(name + " give up");
    			}
    		}
    
    	}

    打印的结果:

    bb entercurrentvalue=true
    bb working
    bb leave
    aa entercurrentvalue=true
    aa working
    aa leave
    

    CountDownLatch

    一个同步辅助类。在完毕一组正在其它线程中运行的操作之前,它同意一个或多个线程一直等待。

    假设设置  final CountDownLatch end = new CountDownLatch(10);  end.countDown();能够降低计数

    假设在某个地方写  end.await();  假设计数不为0,全部线程会一直等待,计数不会被重置


    private static  CountDownLatch mLatch = new CountDownLatch(5);
    	
    	public static void main(String[] args) throws InterruptedException {
    
    
            final ExecutorService exec = Executors.newFixedThreadPool(10);  
    
            for (int index = 0; index < 5; index++) {
                final int NO = index + 1;  
                Runnable run = new Runnable() {
                    public void run() {  
                        try {  
                        	System.out.println(NO + " working");
            				Thread.sleep(2000);
                        } catch (InterruptedException e) {  
                        } finally {  
                        	mLatch.countDown();
                        }  
                    }  
                };  
                exec.submit(run);
    
            }  
            mLatch.await();  
            System.out.println("finish");  
            exec.shutdown();  
        }

    结果:

    1 working
    3 working
    2 working
    4 working
    5 working
    finish
    






    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    Cookie 干货
    element-ui 框架中使用 NavMenu 导航菜单组件时,点击一个子菜单会出现多个子菜单同时展开或折叠?
    数组遍历的方法
    前端网页字体
    样式小收藏:完成、错误、提示动态图标样式
    多语言网站利器 rel="alternate" hreflang="x"
    网页中文章显示一部分,然后“查看全文”
    仿水滴筹中快捷留言祝福、随机生成祝福
    TypeScript知识点
    前端项目经验
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4725195.html
Copyright © 2020-2023  润新知