go4it

just do it

2009年5月7日

模式4-Balking

摘要: 参考《java多线程设计模式》 当警戒条件不成立就马上中断。 // 若有资料修改,就存储到挡安里 public synchronized void save() throws IOException { if (!changed) { System.out.println(Thread.currentThread().getName() + " balks"); return; } doSave(... 阅读全文

posted @ 2009-05-07 23:49 cxccbv 阅读(259) 评论(0) 推荐(0)

模式3-GuardedSuspension

摘要: 参考《java多线程设计模式》 要求线程等候,以保障实例安全。 一个server线程取出请求进行处理,一个client线程放进请求待处理。 public class RequestQueue { private final LinkedList queue = new LinkedList(); public synchronized Request getRequest() { while... 阅读全文

posted @ 2009-05-07 22:29 cxccbv 阅读(246) 评论(0) 推荐(0)

模式2-Immutable

摘要: 来至《java多线程设计模式》 一、方法:无需synchronized方法,本身就不变,没有提供可以让外界修改的方法,只有读取。public final class Person { private final String name; private final String address; public Person(String name, String address) { this.n... 阅读全文

posted @ 2009-05-07 10:21 cxccbv 阅读(228) 评论(0) 推荐(0)

模式1--SingleThreadExecution

摘要: 来至《java多线程设计模式》 一、方法:用synchronized方法,每次只让一个通过。 public class UserThread extends Thread { private final Gate gate; private final String myname; private final String myaddress; public UserThread(Gate g... 阅读全文

posted @ 2009-05-07 10:08 cxccbv 阅读(246) 评论(0) 推荐(0)

Thread-Specific Storage Pattern

摘要: 手工关闭logpublic class ClientThread extends Thread { public ClientThread(String name) { super(name); } public void run() { System.out.println(getName() + " BEGIN"); for (int i = 0; i < 10; i++) { Log.... 阅读全文

posted @ 2009-05-07 09:08 cxccbv 阅读(266) 评论(0) 推荐(0)

java多线程中断学习

摘要: 来至《java多线程设计模式》 1.wait    (1)调用wait方法必须先获得对象实例的锁;    (2)可以再synchronized方法中,或synchronized块中或两者调用的别的方法中;    (3)执行wait方法的线程会进入obj的wait set里面,进入之后就已经释放obj的锁;   ... 阅读全文

posted @ 2009-05-07 09:07 cxccbv 阅读(354) 评论(0) 推荐(0)

导航