go4it

just do it

上一页 1 ··· 7 8 9 10 11 12 13 14 15 ··· 25 下一页

2009年5月8日

模式10--TwoPhaseTermination

摘要: 来至《java多线程设计模式》 两阶段终止,作业中--》终止处理中--》结束 不把处理一半的事情搁着,草率结束,等待其处理好后才真正的结束。public class Main { public static void main(String[] args) { System.out.println("main: BEGIN"); try { CountupThread t = new Countu... 阅读全文

posted @ 2009-05-08 23:45 cxccbv 阅读(240) 评论(0) 推荐(0)

模式9--Future

摘要: 来至《java多线程设计模式》 对于一个执行起来需要花些时间的方法,不等待执行结果出来,而是获取一张替代的“提货单”。 使用异步方法调用,分离准备返回值和使用返回值。 public class Main { public static void main(String[] args) { System.out.println("main BEGIN"); Host hos... 阅读全文

posted @ 2009-05-08 23:35 cxccbv 阅读(236) 评论(0) 推荐(0)

模式8--WorkerThread

摘要: 来至《java多线程设计模式》 类似ThreadPool,预先产生几个worker线程准备工作。 public void startWorkers() { for (int i = 0; i < threadPool.length; i++) { threadPool[i].start(); } }package Sample;public class WorkerThread exten... 阅读全文

posted @ 2009-05-08 23:27 cxccbv 阅读(245) 评论(0) 推荐(0)

模式7--ThreadPerMessage

摘要: 来至《java多线程设计模式》 对每个命令或请求分配一个线程,由这个线程执行工作。 public class Host {     private final Helper helper = new Helper();     public void request(final int count, final char c) { &#... 阅读全文

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

模式6--ReadWriteLock

摘要: 来至《java多线程设计模式》 自己提供一个逻辑锁代替JDK的物理锁synchronized 优点:1.对read操作不进行共享互斥,可以进行多个read操作,提高系统性能         2.适合read》write的情况 package Sample;public final class ReadWriteLock ... 阅读全文

posted @ 2009-05-08 22:55 cxccbv 阅读(311) 评论(0) 推荐(0)

模式5-Producer-Consumer

摘要: 参考《java多线程设计模式》 在多个生产者和多个消费者之间加入一个中间者来缓冲线程之间的处理速度差。 // 放置蛋糕 public synchronized void put(String cake) throws InterruptedException { System.out.println(Thread.currentThread().getName() + " puts ... 阅读全文

posted @ 2009-05-08 22:45 cxccbv 阅读(297) 评论(0) 推荐(0)

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)

上一页 1 ··· 7 8 9 10 11 12 13 14 15 ··· 25 下一页

导航