直奔主题, Single Thread Execution也称作Critical Section(临界区),范例如下:
public class SingleThreadGate { public static void main(String[] args) { System.out.println("ctrl + c to exit."); Gate gate = new Gate(); new UserThread(gate, "Alice", "Alaska").start(); new UserThread(gate, "Bobby", "Brazil").start(); new UserThread(gate, "Chris", "Canada").start(); } } class Gate{ private int count; private String name; private String address; public void pass(String name, String address){ this.count++; this.name=name; this.address=address; check(); } public void check() { if (name.charAt(0) != address.charAt(0)) { System.out.println("*******BROKEN *******" + toString()); } } @Override public String toString() { return "No." + count + " " + name + ", " + address; } } class UserThread extends Thread{ private Gate gate; private String name; private String address; public UserThread(Gate gate, String name, String address){ this.gate=gate; this.name=name; this.address=address; } @Override public void run() { System.out.println(name+":begin->"); while(true){ gate.pass(name,address); Thread.yield(); } } }
执行之后,会发现ou一很多broken,因为在执行pass的时候成员变量被其他线程改变了,所以判断name和address不一致,另外toString方法也存在同样的问题,执行过程中 name和address也随时被其他线程改变,所以会导致同一条记录的name和address也不一致,结果如下:
ctrl + c to exit. Alice:begin-> Chris:begin-> Bobby:begin-> *******BROKEN *******No.306 Bobby, Brazil *******BROKEN *******No.487 Chris, Canada *******BROKEN *******No.624 Bobby, Brazil *******BROKEN *******No.6956 Bobby, Brazil *******BROKEN *******No.7487 Alice, Canada *******BROKEN *******No.7756 Chris, Alaska *******BROKEN *******No.8010 Alice, Canada *******BROKEN *******No.8288 Bobby, Brazil *******BROKEN *******No.8550 Chris, Alaska *******BROKEN *******No.8910 Bobby, Brazil *******BROKEN *******No.9397 Chris, Alaska *******BROKEN *******No.9715 Bobby, Brazil
那么如何改进呢?将pass方法加上synchronized关键字,这样就不会有任何错误了。不过这还不能保证整个gate类的所有方法都是安全的,当调用toString方法时还是会有同样的name与address不一致的问题,所以在toString方法上也要加上synchronized关键字,那么check方法还需要么?答案是不需要,因为check方法时私有的,外部无法访问,而且check方法是被pass方法调用的,而pass方法已经同步了,一个类中的synchronized关键之都是锁定的同一对象(this),所以不需要二次锁定,浪费性能。
synchnronized关键字也可以理解成把方法变成原子性操作,在java中,基本数据类型(primitive)如int, char, byte等都是原子性操作的,但是long和double则不一定,所以我们想把long和double这类非原子性类型按照原子性方式操作,需要加上关键字volatile,这样就可以了。