public class ThreadDeadlock {
ExecutorService exec = Executors.newSingleThreadScheduledExecutor();
// ExecutorService exec = Executors.newCachedThreadPool(); //如果添加给线程池中添加足够多的线程,就可以让所有任务都执行,避免饥饿死锁。
/**
* 模拟页面加载的例子
*
* 产生死锁分析:
* RenderPageTask任务中有2个子任务分别是“加载页眉”和“加载页脚”。当提交RenderPageTask任务时,实际上是向线程池中添加了3个任务,
* 但是由于线程池是单一线程池,同时只会执行一个任务,2个子任务就会在阻塞在线程池中。而RenderPageTask任务由于得不到返回,也会
* 一直堵塞,不会释放线程资源让子线程执行。这样就导致了线程饥饿死锁。
*
* 在一个Callable任务中,要返回2个子任务
* @author hadoop
*
*/
class RenderPageTask implements Callable<String>{
@Override
public String call() throws Exception {
Future<String> header,footer;
header = exec.submit(new Callable<String>(){
@Override
public String call() throws Exception {
System.out.println("加载页眉");
Thread.sleep(2*1000);
return "页眉";
}
}
);
footer = exec.submit(new Callable<String>(){
@Override
public String call() throws Exception {
System.out.println("加载页脚");
Thread.sleep(3*1000);
return "页脚";
}
}
);
System.out.println("渲染页面主体");
return header.get() + footer.get();
}
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
ThreadDeadlock td = new ThreadDeadlock();
Future<String> futre = td.exec.submit(td.new RenderPageTask());
String result = futre.get();
System.out.println("执行结果为:" + result);
}
}
public class Synchronizer{
public synchronized void doSynchronized(){
//do a lot of work which takes a long time
}
}
public class Synchronizer{
Lock lock = new Lock();
public void doSynchronized() throws InterruptedException{
this.lock.lock();
//critical section, do a lot of work which takes a long time
this.lock.unlock();
}
}
public class Lock{
private Boolean isLocked = false;
private Thread lockingThread = null;
public synchronized void lock() throws InterruptedException{
while(isLocked){
wait();
}
isLocked = true;
lockingThread = Thread.currentThread();
}
public synchronized void unlock(){
if(this.lockingThread != Thread.currentThread()){
throw new IllegalMonitorStateException(
"Calling thread has not locked this lock");
}
isLocked = false;
lockingThread = null;
notify();
}
}
public class FairLock {
private Boolean isLocked = false;
private Thread lockingThread = null;
private List<QueueObject> waitingThreads =
new ArrayList<QueueObject>();
public void lock() throws InterruptedException{
QueueObject queueObject = new QueueObject();
Boolean isLockedForThisThread = true;
synchronized(this){
waitingThreads.add(queueObject);
}
while(isLockedForThisThread){
synchronized(this){
isLockedForThisThread =
isLocked || waitingThreads.get(0) != queueObject;
if(!isLockedForThisThread){
isLocked = true;
waitingThreads.remove(queueObject);
lockingThread = Thread.currentThread();
return;
}
}
try{
queueObject.doWait();
}
catch(InterruptedException e){
synchronized(this) {
waitingThreads.remove(queueObject);
}
throw e;
}
}
}
public synchronized void unlock(){
if(this.lockingThread != Thread.currentThread()){
throw new IllegalMonitorStateException(
"Calling thread has not locked this lock");
}
isLocked = false;
lockingThread = null;
if(waitingThreads.size() > 0){
waitingThreads.get(0).doNotify();
}
}
}
public class QueueObject {
private Boolean isNotified = false;
public synchronized void doWait() throws InterruptedException {
while(!isNotified){
this.wait();
}
this.isNotified = false;
}
public synchronized void doNotify() {
this.isNotified = true;
this.notify();
}
public Boolean equals(Object o) {
return this == o;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有