Java Concurrent Topics
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 ForkJoinTask
s, 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的更多相关文章
- java concurrent包的学习(转)
java concurrent包的学习(转) http://my.oschina.net/adwangxiao/blog/110188 我们都知道,在JDK1.5之前,Java中要进行业务并发时,通常 ...
- How to Create a Java Concurrent Program
In this Document Goal Solution Overview Steps in writing Java Concurrent Program Template ...
- Java中编写线程安全代码的原理(Java concurrent in practice的快速要点)
Java concurrent in practice是一本好书,不过太繁冗.本文主要简述第一部分的内容. 多线程 优势 与单线程相比,可以利用多核的能力; 可以方便的建模成一个线程处理一种任务; 与 ...
- Java Concurrent之 AbstractQueuedSynchronizer
ReentrantLock/CountDownLatch/Semaphore/FutureTask/ThreadPoolExecutor的源码中都会包含一个静态的内部类Sync,它继承了Abstrac ...
- [Java Concurrent] 多线程合作 producer-consumers / queue 的简单案例
在多线程环境下,通过 BlockingQueue,实现生产者-消费者场景. Toast 被生产和消费的对象. ToastQueue 继承了 LinkedblockingQueue ,用于中间存储 To ...
- [Java Concurrent] 多线程合作 wait / notifyAll 的简单案例
本案例描述的是,给一辆汽车打蜡.抛光的场景. Car 是一辆被打蜡抛光的汽车,扮演共享资源的角色. WaxOnCommand 负责给汽车打蜡,打蜡时需要独占整部车,一次打一部分蜡,等待抛光,然后再打一 ...
- [Java Concurrent] 并发访问共享资源的简单案例
EvenGenerator 是一个偶数生成器,每调用一个 next() 就会加 2 并返回叠加后结果.在本案例中,充当被共享的资源. EvenChecker 实现了 Runnable 接口,可以启动新 ...
- 利用java concurrent 包实现日志写数据库的并发处理
一.概述 在很多系统中,往往需要将各种操作写入数据库(比如客户端发起的操作). 最简单的做法是,封装一个公共的写日志的api,各个操作中调用该api完成自己操作日志的入库.但因为入数据库效率比较低,如 ...
- [Java concurrent][Collections]
同步容器类 同步容器类包括Vector和Hashtable,二者是早期JDK的一部分.以及一些在JDK1.2中添加的可以由Collections.synchronizedXxx等工厂方法创建的. 这些 ...
随机推荐
- HTML中的<audio>和<video>标签讲解
关于<audio>标签讲解:点击这里 这里来一段简单的html5的音频代码: <audio src="someaudio.wav"> 您的浏览器不支持 au ...
- luci-bwc(记录流量功能)
使用方法如下: root@openwrt:/# luci-bwcUsage: luci-bwc [-t timeout] -i ifname // ifname为 ...
- ubuntu 调试库
.安装带有调试信息的libc: sudo apt-get install libc6-dbg .下载libc源码 a.选定一个放置源码的目录并进入,如 /home/kent/dev-os/libc6- ...
- Delphi中,FALSE 和 nil ,true 和 nil,0的区别
True和False是布尔型(Boolean)的值,就是"是"或"否"的意思.nil就是空,一般用于指针或对象变量,指对针或对象对象一般初始化为nil或者释放后 ...
- 第二次冲刺spring会议(第一次会议)
[例会时间]2014/5/4 21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇 内部测试版发布时间5月11日 ...
- 10.按要求编写Java应用程序。 (1)创建一个叫做People的类: 属性:姓名、年龄、性别、身高 行为:说话、计算加法、改名 编写能为所有属性赋值的构造方法; (2)创建主类: 创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80; 让该对象调用成员方法: 说出“你好!” 计算23+45的值 将名字改为“李四”
package com.hanqi.test; public class People { private String name,sex; private int age; private doub ...
- 如何直观的解释back propagation算法?
转自:知乎-https://www.zhihu.com/question/27239198 作者:匿名用户链接:https://www.zhihu.com/question/27239198/answ ...
- Meta标签中的属性及含义
一.Meta标签中的format-detection属性及含义 format-detection翻译成中文的意思是“格式检测”,顾名思义,它是用来检测html里的一些格式的,那关于meta的forma ...
- 正则表达式:reg.test is not a function
正则中 比如 var reg = "/^[0-9]$/" 会报 reg.test is not a function 如果 var reg = /^[0-9]$/ 就不会有错 因为 ...
- 自定义silverlight中datagrid的排序事件
<sdk:DataGrid AutoGenerateColumns="False" CanUserSortColumns="False" CanUserR ...