Advertisement
Advertisement
| 02.12.2008 at 04:53PM PST, ID: 23158355 |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
|
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
| Microsoft |
| Apple |
| Internet |
| Gamers |
| Digital Living |
| Virus & Spyware |
| Hardware |
| Software |
| ITPro |
| Developer |
| Storage |
| OS |
| Database |
| Security |
| Programming |
| Web Development |
| Networking |
| Other |
| Community Support |
| 02.13.2008 at 05:03AM PST, ID: 20883752 |
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: |
programs/thread/Philosopher.java
// Philosopher
import java.util.Random;
/** the Dining Philosophers.
*/
public class Philosopher extends Thread {
/** the fork between two philosophers.
*/
protected static class Fork {
protected int me; // number for trace
protected boolean inUse; // true if fork is in use
public Fork (int me) {
this.me = me;
}
/** returns true if fork is obtained, false if not.
*/
public synchronized boolean get (int who) {
System.err.println(who+(inUse ? " misses " : " grabs ")+me);
return inUse ? false : (inUse = true);
}
/** drops the fork.
*/
public synchronized void put (int who) {
System.err.println(who+" drops "+me);
inUse = false; notify();
}
/** returns once fork is obtained.
*/
public synchronized void waitFor (int who) {
while (! get(who))
try {
wait();
} catch (InterruptedException e) { e.printStackTrace(); }
}
}
The new idea is conditional access to the fork which otherwise acts as a semaphore.
The forks are distributed in main():
programs/thread/Philosopher.java
/** make one diner.
*/
public Philosopher (int me, Fork left, Fork right) {
this.me = me; this.left = left; this.right = right;
}
protected static Random random = new Random(); // randomize
protected int me; // number for trace
protected Fork left, right; // my forks
/** philosopher's body: think and eat 5 times.
*/
public void run () {
for (int n = 1; n <= 5; ++ n) {
System.out.println(me+" thinks");
try {
Thread.sleep((long)(random.nextFloat()*1000));
} catch (InterruptedException e) { e.printStackTrace(); }
for (;;)
try {
left.waitFor(me);
if (right.get(me)) {
System.out.println(me+" eats");
try {
Thread.sleep((long)(random.nextFloat()*1000));
} catch (InterruptedException e) { e.printStackTrace(); }
right.put(me);
break;
}
} finally {
left.put(me);
Thread.yield(); // necessary to reschedule sparc ultra...
}
}
System.out.println(me+" leaves");
}
/** sets up for 5 philosophers.
*/
public static void main (String args []) {
Fork f[] = new Fork[5];
for (int n = 0; n < 5; ++ n) f[n] = new Fork(n);
Philosopher p[] = new Philosopher[5];
p[0] = new Philosopher(0, f[4], f[0]); // backwards
for (int n = 1; n < 5; ++ n) p[n] = new Philosopher(n, f[n-1], f[n]);
for (int n = 0; n < 5; ++ n) p[n].start();
}
}
|
| 02.13.2008 at 06:41AM PST, ID: 20884470 |
| 02.13.2008 at 10:39AM PST, ID: 20886813 |
| 02.13.2008 at 11:16AM PST, ID: 20887125 |