To prevent Memory Consistency Errors(MCEs), it is good practice to specify synchronized class specifier, and mark all the related methods as synchronized. This solves the MCEs, but not perfectly in its inefficiency. Note if 2 methods marked synchronized in an instance, they cannot be running at the same time, however, in these 2 methods, not all the procedures are possible to get MCEs, so in some big & complex classes|methods, we use Atomic Variables instead as follows to cut the should be synchronized part into pieces (implements Compare and Swap + volatile and native) the cas method is not well working under intensive competitive:

 import java.util.concurrent.atomic.AtomicInteger;

 class AtomicCounter {
private AtomicInteger c = new AtomicInteger(0); public void increment() {
c.incrementAndGet();
} public void decrement() {
c.decrementAndGet();
} public int value() {
return c.get();
} }

Concurrent Random Numbers:

In java.util.concurrent package, class ThreadLocalRandom, is constructed to use random numbers from multiple threads or ForkJoinTasks, use ThreadLocalRandom instead of Math.random(), to get better performance. If need cryptologilly safe, use SecureRandom class.

Call ThreadLocalRandom.current(); after that, call one of  the methods, nextX() to retrieve a random number. Every thread you need to instance a ThreadLocalRandom obj.

nextBoolean(), nextLong(), nextGaussian() ...

Further Reading in Java Concurrency:

  • Concurrent Programming in Java: Design Principles and Pattern (2nd Edition) by Doug Lea. A comprehensive work by a leading expert, who's also the architect of the Java platform's concurrency framework.
  • Java Concurrency in Practice by Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea. A practical guide designed to be accessible to the novice.
  • Effective Java Programming Language Guide (2nd Edition) by Joshua Bloch. Though this is a general programming guide, its chapter on threads contains essential "best practices" for concurrent programming.
  • Concurrency: State Models & Java Programs (2nd Edition), by Jeff Magee and Jeff Kramer. An introduction to concurrent programming through a combination of modeling and practical examples.
  • Java Concurrent Animated: Animations that show usage of concurrency features.

Java Concurrent Topics的更多相关文章

  1. java concurrent包的学习(转)

    java concurrent包的学习(转) http://my.oschina.net/adwangxiao/blog/110188 我们都知道,在JDK1.5之前,Java中要进行业务并发时,通常 ...

  2. How to Create a Java Concurrent Program

    In this Document   Goal   Solution   Overview   Steps in writing Java Concurrent Program   Template ...

  3. Java中编写线程安全代码的原理(Java concurrent in practice的快速要点)

    Java concurrent in practice是一本好书,不过太繁冗.本文主要简述第一部分的内容. 多线程 优势 与单线程相比,可以利用多核的能力; 可以方便的建模成一个线程处理一种任务; 与 ...

  4. Java Concurrent之 AbstractQueuedSynchronizer

    ReentrantLock/CountDownLatch/Semaphore/FutureTask/ThreadPoolExecutor的源码中都会包含一个静态的内部类Sync,它继承了Abstrac ...

  5. [Java Concurrent] 多线程合作 producer-consumers / queue 的简单案例

    在多线程环境下,通过 BlockingQueue,实现生产者-消费者场景. Toast 被生产和消费的对象. ToastQueue 继承了 LinkedblockingQueue ,用于中间存储 To ...

  6. [Java Concurrent] 多线程合作 wait / notifyAll 的简单案例

    本案例描述的是,给一辆汽车打蜡.抛光的场景. Car 是一辆被打蜡抛光的汽车,扮演共享资源的角色. WaxOnCommand 负责给汽车打蜡,打蜡时需要独占整部车,一次打一部分蜡,等待抛光,然后再打一 ...

  7. [Java Concurrent] 并发访问共享资源的简单案例

    EvenGenerator 是一个偶数生成器,每调用一个 next() 就会加 2 并返回叠加后结果.在本案例中,充当被共享的资源. EvenChecker 实现了 Runnable 接口,可以启动新 ...

  8. 利用java concurrent 包实现日志写数据库的并发处理

    一.概述 在很多系统中,往往需要将各种操作写入数据库(比如客户端发起的操作). 最简单的做法是,封装一个公共的写日志的api,各个操作中调用该api完成自己操作日志的入库.但因为入数据库效率比较低,如 ...

  9. [Java concurrent][Collections]

    同步容器类 同步容器类包括Vector和Hashtable,二者是早期JDK的一部分.以及一些在JDK1.2中添加的可以由Collections.synchronizedXxx等工厂方法创建的. 这些 ...

随机推荐

  1. hdu_5879_Cure(打表)

    题目链接:hdu_5879_Cure 题意: 给你一个n,让你计算1/k2的和,k从1到n. 题解: 因为只保留5位小数,所以打个100W的表,比这个数大的直接输出最后一位就行了 #include&l ...

  2. 1、java面试

    1.为什么用单例而不用static 答案:首先你要明白static是在什么时候初始化的,其设计意图是什么,单例就是我们运行的当前虚拟机中有且只有一个需要的对象,不存在重复.static是给类静态成员变 ...

  3. 如何提升 CSS 选择器性能

    CSS 选择器性能损耗来自? CSS选择器对性能的影响源于浏览器匹配选择器和文档元素时所消耗的时间,所以优化选择器的原则是应尽量避免使用消耗更多匹配时间的选择器.而在这之前我们需要了解CSS选择器匹配 ...

  4. openwrt增加串口登录需要密码

    https://wiki.openwrt.org/doc/howto/serial.console.password Openwrt 串口默认是没有密码的.Openwrt启动后,一个默认的密码将被启用 ...

  5. Android 切横竖屏时走的生命周期方法?222

    第一种情况: 不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次 第二种情况: 设置Activity的androi ...

  6. HUST 1339 Reversal(字符串)

    题目链接 题解:将每个单词倒置,可以用char数组,然后用空格分隔,这里用的是string和stringstream. #include <cstdio> #include <ios ...

  7. [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力

    Couple Cover Time Limit: 3000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u Descri ...

  8. ubuntu切换到超级管理员权限

    默认情况下是无法切换的,需要给root用户设置上密码 mars@mars-LIFEBOOK-LH531:~$ sudo passwd root[sudo] password for mars: 输入新 ...

  9. 【Machine Learning in Action --5】逻辑回归(LogisticRegression)

    1.概述 Logistic regression(逻辑回归)是当前业界比较常用的机器学习方法,用于估计某种事物的可能性. 在经典之作<数学之美>中也看到了它用于广告预测,也就是根据某广告被 ...

  10. NSURL访问项目中的文件

    最近在研究视频处理,具体为:将一个mp4文件,拖入项目工程中,通过url访问文件. 开始代码如下: NSString *path = [[NSBundle mainBundle]pathForReso ...