摘要:Program order rule. Each action in a thread happens-before every action in that thread that comes later in the program order.Monitor lock rule. An unlock on a monitor lock happens-before every subsequent lock on that same monitor lock.Volatile variable rule. A write to a volatile field happens-befor
阅读全文
摘要:Perhaps surprisingly, concurrent programming isn’t so much about threads or locks, any more than civil engineering is about rivets and I-beams. Of course,building bridges that don’t fall down requires the correct use of a lot of rivets and I-beams, just as building concurrent programs require the co
阅读全文
摘要:Preface前言At this writing, multicore processors are just now becoming inexpensive enough for midrange desktop systems. Not coincidentally, many development teams are noticing more and more threading-related bug reports in their projects. In a recent post on the NetBeans developer site, one of the cor
阅读全文
摘要:单例模式在Java编程实践中经常用到。单例模式用于保证每个类只有一个实例存在。本文将对Java单利模式进行一个系统深入的介绍。单例模式是为了保证一个类只有一个实例存在。注意,我们所说的每个类只有一个实例存在是相对于一个JVM和一个ClassLoader而言的。由于不同的类装载器装载的类位于不同的命名空间内,所以在同一个JVM还是可以有有不同的类装载器装载的不同的实例。但这些实例是不同的。测试:先建立如下Singleton类:public class Singleton { private static Singleton instance = new Singleton(); ...
阅读全文
摘要:问题:请问下面的那种方法能正确地把字符"B"从集合stringList中删除掉?1 List<String> stringList = new ArrayList<String>();2 stringList.add("A");3 stringList.add("B");4 stringList.add("C");5 stringList.add("B");6 stringList.add("D");7 stringList.add("E&q
阅读全文
摘要:问题:有一个类Foo如下:public class Foo{ private int i = 0; public void f(){ System.println("In f() i=" + i); } public synchronized void g(){ //point 1 System.println("In g() i=" + i); i = 1; //point 2 System.println("In g() i=" + i); }}请问如果有两个线程A,B,线程B执行到point 2时,问线程A总会打印出"
阅读全文
摘要:3.3.3 ThreadLocal3.3.3 ThreadLocalA more formal means of maintaining thread confinement is ThreadLocal,which allows you to associate a per-thread value with a value-holding object. ThreadLocal provides get and set accessor methods that maintain a separate copy of the value for each thread that use...
阅读全文
摘要:Explicit LocksExplicit LockBefore Java 5.0, the only mechanisms for coordinating access to shared data were synchronized and volatile. Java 5.0 adds another option: ReentrantLock.Contrary to what some have written, ReentrantLock is not a replacement for intrinsic locking, but rather an alternative w
阅读全文