package org.rui.thread.deadlock; /** * 死鎖 * * 筷子 * * @author lenovo * */ public class Chopstick { private boolean taken = false; public synchronized void take() throws InterruptedException { while (taken) { wait(); } taken = true; } public synchronized void drop() { taken = false; notifyAll(); } }
package org.rui.thread.deadlock; import java.util.Random; import java.util.concurrent.TimeUnit; /** * 哲學家 * @author lenovo * */ public class Philosopher implements Runnable { private Chopstick left; private Chopstick right; private final int id; private final int ponderFactor;// 思考 private Random rand = new Random(47); private void pause() throws InterruptedException { if (ponderFactor == 0) return; { TimeUnit.MILLISECONDS.sleep(rand.nextInt(ponderFactor * 250)); } } public Philosopher(Chopstick left, Chopstick right, int id, int ponderFactor) { this.left = left; this.right = right; this.id = id; this.ponderFactor = ponderFactor; } @Override public void run() { try { while (!Thread.interrupted()) { System.out.println(this + " " + "thinking 思考"); pause();// 暂停 // philosopher becomes hungry System.out.println(this + " " + "grabbing 抓 right"); right.take(); System.out.println(this + " " + "grabbing 抓 left"); left.take(); System.out.println(this + "" + "eating 吃饭"); pause();// 暂停 right.drop();//终止 left.drop(); } } catch (InterruptedException e) { System.out.println(this+" "+"通过中断退出"); } } @Override public String toString() { return " philosopher " + id; } }
package org.rui.thread.deadlock; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** * 這個程序將產生死鎖 * @author lenovo * */ public class DeadlockingDiningPhilosophers { public static void main(String[] args) throws InterruptedException, IOException { String arg[] = { "0", "5","timeout"}; int ponder = 5; if (arg.length > 0) { ponder = Integer.parseInt(arg[0]); } int size = 5; if (arg.length > 1) { size = Integer.parseInt(arg[1]); } ExecutorService exec = Executors.newCachedThreadPool(); Chopstick[] sticks = new Chopstick[size]; for (int i = 0; i < size; i++) { sticks[i] = new Chopstick(); } for (int i = 0; i < size; i++) { exec.execute(new Philosopher(sticks[i], sticks[(i + 1) % size], i, ponder)); if(arg.length==3&&arg[2].equals("timeout")){ TimeUnit.SECONDS.sleep(5); } else{ System.out.println("press enter to quit"); System.in.read(); } exec.shutdownNow(); } } }
package org.rui.thread.deadlock; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** * 破壞第四個條件 防止死鎖 * @author lenovo * */ public class FixedDiningPhilosophers { public static void main(String[] args) throws InterruptedException, IOException { String[] arg = { "5", "5", "timeout" }; int ponder = 5; if (arg.length > 0) { ponder = Integer.parseInt(arg[0]); } int size = 5; if (arg.length > 1) { size = Integer.parseInt(arg[1]); } ExecutorService exec = Executors.newCachedThreadPool(); Chopstick[] sticks = new Chopstick[size]; for (int i = 0; i < size; i++) { sticks[i] = new Chopstick(); } for (int i = 0; i < size; i++) { // 通过确保最后一个philosopher先拿起和放下左边的Chopstick, // 我们可以移除死锁,从而使这个程序平滑的运行。 if (i < (size - 1)) { exec.execute(new Philosopher(sticks[i], sticks[i + 1], i, ponder)); } else { exec.execute(new Philosopher(sticks[0], sticks[i], i, ponder)); } } if (arg.length == 3 && arg[2].equals("timeout")) { TimeUnit.SECONDS.sleep(5); } else { System.out.println("press enter to quit"); System.in.read(); } exec.shutdownNow(); } }
java 线程 死锁(哲学家用餐案例讲解) -------thinking java 4
原文地址:http://blog.csdn.net/liangrui1988/article/details/39433275