多线程--原子类理解
2016-10-27 14:19
225 查看
package cn.stu; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; public class Main { public static AtomicInteger index = new AtomicInteger(0); public static int count; public static void main(String[] args) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(10000); ExecutorService service = Executors.newFixedThreadPool(10000); for(int i = 0; i<10000;i++){ service.submit(new Runnable() { @Override public void run() { // 使用非阻塞算法来实现并发控制。 // 如果我们用 Lock的形式需要的还是阻塞式来处理,一条线程形式来一次加1的模式。 index.getAndIncrement(); count++; latch.countDown(); } }); } latch.await(); System.out.println("运行结果:---->"+index.intValue()+"-----普通数据--->"+count); service.shutdown(); } }
运行结果:
运行结果:---->10000-----普通数据--->9996
前面的10000是不会改变的。 后面的9996会进行改变。
相关文章推荐
- Android多线程研究(8)——Java中的原子性理解
- Android多线程研究(8)——Java中的原子性理解
- 【深入理解JAVA多线程】(4)Java多线程之原子性、可见性、有序性
- Android多线程研究(8)——Java中的原子性理解
- java多线程中对于join方法的理解
- 通通透透理解ThreadLocal,实现安全的多线程
- 多线程加深理解_进攻五个城
- Java多线程系列--“JUC原子类”03之 AtomicLongArray原子类
- Java 多线程编程之五:一个理解 wait() 与 notify() 的例子
- 移动互联网再理解:从比特世界向原子世界的回迁(虎嗅网)
- JavaScript可否多线程? 深入理解JavaScript定时机制
- [转]JavaScript可否多线程? 深入理解JavaScript定时机制
- 从JAVA多线程理解到集群分布式和网络设计的浅析
- 深入理解Java多线程中的wait(),notify()和sleep()
- JavaScript是否可实现多线程 深入理解JavaScript定时机制
- 深入理解Java多线程中的wait(),notify()和sleep()
- 关于并发,多线程的一些理解
- 对于多线程的理解以及我们为什么要使用多线程
- 多线程理解杂记
- 理解Android多线程里面三种任务Runnable和Callable和FutureTask的用法