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. 浙大 pat 1024题解

    1024. Palindromic Number (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  2. heap和stack的区别

    参考<程序员面试宝典> 1.栈区(stack) 由编译器自动分配和释放,存放函数的参数值,局部变量值等.其操作方式类似于数据中的栈. 2.堆区(heap) 一般由程序员分配和释放,若程序员 ...

  3. Python库 - import matplotlib.pyplot as plt 报错问题

    为了避免各种问题,请使用最新的2.7.13安装文件   1.先设置好环境变量 在path变量中设置好以下路径: C:\Python27\Scripts C:\Python27    2.大部分报错问题 ...

  4. Win7的64位系统如何搭建安卓Android开发环境

    在搭建安卓Android开发环境,那么现在比较主流的Win7的64位操作系统如何搭建呢?其实很简单,不需要设置任何环境变量,只需要下载两个程序包(ADT和JDK),下载的时候注意选择相应的64位程序包 ...

  5. Linux下gcc编译生成动态链接库*.so文件并调用它【转载】

    动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...

  6. 深入体会__cdecl与__stdcall

    在学习C++的过程中时常碰到WINAPI或者CALLBACK这样的调用约定,每每觉得十分迷惑.究竟这些东西有什么用?不用他们又会不会有问题?经过在网上的一番搜寻以及自己动手后,整理成以下的学习笔记.1 ...

  7. java程序使用memcached

    Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. 1.linux环境下安装与启动memcache: 以编译安装的方式安装.具体参看: http://wang ...

  8. sublime3安装

    1.下载安装程序 http://c758482.r82.cf2.rackcdn.com/Sublime%20Text%20Build%203059%20Setup.exe 2.下载注册机 http:/ ...

  9. sd卡文件操作

    1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...

  10. iOS可变字符串的所有操作

    可直接把代码复制即可结合输出看结果加深记忆 //创建一个可变字符串 NSMutableString * ms1 = [[NSMutableString alloc]init]; //可以通过类方法来创 ...