高天乐 welcome you

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

2013年9月7日

摘要: 当线程“读取”实例的状态时,实例的状态不会改变,只有线程对实例“写入”操作时才会改变。read-write lock 模式将读取和写入分开来处理,在读取数据前获取读锁定,而写入之前,必须获取写锁定。public class ReadWriteThreadTest { public static void main(String[] args) { Data data = new Data(5); new ReaderThread(data).start(); new ReaderThread(data).start(); new R... 阅读全文
posted @ 2013-09-07 23:33 高天乐 阅读(589) 评论(0) 推荐(0) 编辑

摘要: 这个模式跟Guarded模式有点类似,不过需要一个控制台限制请求方和处理方的频度和数量。public class ProducerConsumerTest { /** * @param args */ public static void main(String[] args) { Table table = new Table(3); new MakerThread("MakerThread1", table , 1000).start(); new MakerThread("MakerThread2", ... 阅读全文
posted @ 2013-09-07 20:56 高天乐 阅读(684) 评论(0) 推荐(0) 编辑

摘要: word自动保存功能,如果文档被修改了,后台线程每隔一段时间会自动执行保存功能,但是如果用户在自动保存之前用Ctrl+S手动保存呢?自动保存还会执行吗?答案是不会,因为这个操作时不需要重复做的。public class BalkingTest { public static void main(String[] args) { Data data = new Data("data.txt","(empty)"); new ChangerThread("ChangerThread", data).start(); new AutoSav 阅读全文
posted @ 2013-09-07 20:16 高天乐 阅读(860) 评论(0) 推荐(0) 编辑

摘要: 线程在运行过程中需要停下来等待,然后再继续执行。范例程序,一个线程(ClientThread)对另外一个线程(ServerThread)传递请求,实现一个模拟消息通道的功能。public class GuardedSuspensionTest { public static void main(String[] args) { RequestQueue queue = new RequestQueue(); ClientThread ct = new ClientThread(queue, "Alice's Client"); ServerThread st = n 阅读全文
posted @ 2013-09-07 19:30 高天乐 阅读(692) 评论(0) 推荐(0) 编辑

摘要: 上一章讲的是线程互斥的synchronized实现,这样做会影响性能,如何才能做到既不影响性能又能达到线程安全的目的呢,就是使用状态绝不会改变的类,Java中的应用就是String类。public class ImmutableThreadTest { /** * @param args */ public static void main(String[] args) { Person p = new Person("Alice", "Alaska"); new PrintPersonThread(p).start(); new PrintPerson 阅读全文
posted @ 2013-09-07 17:19 高天乐 阅读(606) 评论(0) 推荐(0) 编辑

摘要: 直奔主题, 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, &qu 阅读全文
posted @ 2013-09-07 15:18 高天乐 阅读(1199) 评论(0) 推荐(0) 编辑

摘要: 通过几天的认真阅读,发现这是一本难得一见的好书,为了加深巩固学习成功,我打算将书中的例子全部自己实现一遍,特此记录下来也方便其他朋友学习。第一章,java语言的线程单线程程序:打印10000次good字符串public class SingleThreadSample { public static void main(String[] args) { for(int i=0; i< 10000; i++){ System.out.print("good!"); } }}严格的说并不是只有一个线程在操作,还有其他的线程在非java处理系统上运行,比如gc,gui相关的 阅读全文
posted @ 2013-09-07 11:55 高天乐 阅读(21232) 评论(1) 推荐(2) 编辑