• AtomicInteger的使用


    JDK API 1.7相关介绍

    可以用原子方式更新的 int 值。有关原子变量属性的描述,请参阅 java.util.concurrent.atomic 包规范。AtomicInteger 可用在应用程序中(如以原子方式增加的计数器),并且不能用于替换 Integer。但是,此类确实扩展了 Number,允许那些处理基于数字类的工具和实用工具进行统一访问。

    图片1

    图片2

    AtomicInteger 是线程安全的,多线程对同一个数加100次,结果一定是100. 相关代码示例:

    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.atomic.AtomicInteger;
    
    import javax.annotation.PostConstruct;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
    import org.springframework.stereotype.Component;
    
    /**
     * @ClassName: AtomicIntegerHandle 
     * @Description: AtomicInteger的使用
     * @author wasim
     * @create at 2015-8-12 下午8:44:51
     *  
     */
    @Component
    public class AtomicIntegerHandle {
    	
    	@Autowired
    	ThreadPoolTaskExecutor executor;
    	
    	@PostConstruct
    	public void handleQuestionKnowledge(){
    		
    		AtomicInteger atomicInteger = new AtomicInteger();
    		
    		int round = 15; //要执行的线程总数
    		
    		//导出文件 
    		executor.execute(new ExportStats(atomicInteger, round));
    		
    		//导出文件前,需要先循环下面12个线程
    		for(int i=0;i<round;i++){
    			executor.execute(new similarKnowledgeHandle(atomicInteger));
    		}
    		
    	}
    	
    	
    	public class similarKnowledgeHandle implements Runnable{
    		AtomicInteger atomicInteger;
    		
    		public similarKnowledgeHandle(AtomicInteger atomicInteger) {
    			this.atomicInteger =atomicInteger;
    		}
    		
    		@Override
    		public void run() {
    			System.out.println("do some thing....");
    			atomicInteger.getAndIncrement();
    			System.out.println(atomicInteger.get()); //显示当前计数
    		}
    	}
    	
    	
    	public class ExportStats implements Runnable{
    		
    		AtomicInteger atomicInteger;
    		int round;
    		
    		public ExportStats(AtomicInteger atomicInteger, int round) {
    			this.atomicInteger =atomicInteger;
    			this.round = round;
    		}
    		
    		@Override
    		public void run() {
    			try {
    				boolean flag = true;
    				while(flag){
    					if(atomicInteger.get() == round){
    						flag = false;
    						System.out.println("预处理完成,开始执行相关...");
    					} else {
    						System.out.println("wait...");
    						Thread.sleep(1000);
    					}
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    		
    	}
    }
    
    http://www.wangxin123.com
  • 相关阅读:
    CSS揭秘三(形状)
    CSS揭秘(二背景与边框)
    js数组去重
    Iterator
    ES6数据结构set
    JS浏览器对象(BOM)
    JS 数据类型转换
    js的cookie,localStorage,sessionStorage
    (html+css)云道首页
    蓝桥杯-基础练习 01字串-C语言-5层循环法
  • 原文地址:https://www.cnblogs.com/wangxin37/p/6397164.html
Copyright © 2020-2023  润新知