Single Threaded Execution: 该模式能保证最多有一个线程执行指定的方法
何时使用: 1. 多线程环境
2. 多线程存在共享的资源
3. 共享的资源是可变的
以上3个条件缺一不可
Single Threaded Execution模式如果同时满足下列几个条件时,就会产生死锁:
1. 每个线程 同时需要1个以上共享资源
2. 每个线程的一个操作在锁定一个资源的时候,还没解除前就去锁定另一个资源
3。每个线程对操作对资源需要的顺序不一样
Main:
public class Main {
public static void main(String[] args) {
System.out.println("Testing EaterThread, hit CTRL+C to exit.");
Tool spoon = new Tool("Spoon");
Tool fork = new Tool("Fork");
new EaterThread("Alice", fork, spoon ).start();
new EaterThread("Bobby", spoon, fork).start();
}
}
Tool:
public class Tool {
private final String name;
public Tool(String name) {
this.name = name;
}
public String toString() {
return "[ " + name + " ]";
}
}
EaterThread:
public class EaterThread extends Thread {
private String name;
private final Tool lefthand;
private final Tool righthand;
public EaterThread(String name, Tool lefthand, Tool righthand) {
this.name = name;
this.lefthand = lefthand;
this.righthand = righthand;
}
public void run() {
while (true) {
eat();
}
}
public void eat() {
synchronized (lefthand) {
System.out.println(name + " takes up " + lefthand + " (left).");
synchronized (righthand) {
System.out.println(name + " takes up " + righthand + " (right).");
System.out.println(name + " is eating now, yam yam!");
System.out.println(name + " puts down " + righthand + " (right).");
}
System.out.println(name + " puts down " + lefthand + " (left).");
}
}
}
分享到:
相关推荐
第1章 Single Threaded Execution——能通过这座桥的,只有一个人 第2章 Immutable——想破坏它也没办法 第3章 Guarded Suspension——要等到我准备好喔 第4章 Balking——不需要的话,就算了吧 第5章 Producer-...
第1章 Single Threaded Execution——能通过这座桥的,只有一个人 第2章 Immutable——想破坏它也没办法 第3章 Guarded Suspension——要等到我准备好喔 第4章 Balking——不需要的话,就算了吧 第5章 Producer-...
Java多线程模式详解 目录: 一、漫谈UML Java语言的线程 多线程的评量标准 二、 1、Single Threaded Execution ———— 能通过这座桥的,只有一个人 2、Immutable ———— 想破坏它也没办法 3、Guarded ...
多线程入门,多线程基础知识,描述了Synchronized的线程互斥原理,和Single Threaded Execution模式,符合基础入门的用户可以好好学习,加深理解
对SMP机器上的MPI的实现进行详细讲述,对thread理论有大帮助
正在进行中的一本书,着重介绍如何使用Java语言进行面向对象的多线程设计和编程。
测试无问题,默认字符集请改为utf-8,端口号可以按自己喜好更改,已经准备好iisstart.html文件在其中,可以访问这个网页或是自己新令。
03.多线程的代价Going from a singlethreaded to a multithreaded application doesn't just
Chrome multi-threaded download manager extension,based on Aria2 and AriaNg... Chrome多线程下载扩展。.zip,Chrome multi-threaded download manager extension,based on Aria2 and AriaNg. Chrome多线程下载扩展。
The CSWPFThreading sample ...* This trick is very useful if you want single threaded GUI application, and * also want to keep the GUI responsive when doing expensive operations in * the UI thread.
This book is intended for programmers of all... Therefore, even if you've written a threaded program in Java, this book can help you to exploit new features of Java to write even more effective programs.
c#实现的工作队列,workquere,多线程管理-c# realization of the work queue, workquere, multi-threaded management
用Java作的HTTP1.0 下的Webserver服务器 用TCP协议,采用多线程技术,可作为WEbServer的入门例子
确定数字是否为质数的多线程Java应用程序。 方法 给定数字n和线程数t ,将sqrt(n)的值2均匀划分为t部分。 这些部分由多个线程处理,以检查数字n是否为质数。 样品输出 How many threads do you wish to create? 4 ...
多线程精品资源--Chrome multi-threaded download manager extension,
通过参数threaded_execution,我们可以控制数据库是以多进程方式运行还是以多线程方式运行,默认该参数是false数据库以多进程方式运行。 1.查看参数的默认值,和数据库进程状态. [oracle@ol6 ~]
vc编程HTTP协议多线程下载经典代码vc programming multi-threaded HTTP protocol to download classic code
实例269——使用多线程进行文件搜索的例子。
MFC多线程TCP服务设计经典代码MFC multi-threaded TCP service design classic code
多线程Client/Server C++代码.适合初学者