In main method, I created 5 threads and start() them all. The problem is, even I added a condition to end all threads, they still run. When I add System.out.print(""); below the loop for, my program works well. This is just an accident because I wanna check if my while is working or not. I changed the position of System.out.print(""); but it couldn't work as before.
Can I have some explanations?
public static void main(String args[]) { int number = 5; Philosopher phils[] = new Philosopher[number]; Fork Fork[] = new Fork[number]; for (int i = 0; i < number; i++) { Fork[i] = new Fork(); } for (int i = 0; i < number; i++) { Fork firstFork = Fork[i]; Fork secondFork = Fork[(i + 1) % number]; if (i % 2 == 1) { phils[i] = new Philosopher(firstFork, secondFork); } else { phils[i] = new Philosopher(secondFork, firstFork); } Thread t = new Thread(phils[i], "Philosopher " + i); t.start(); } while (true) { try { boolean allEatten = true; for (Philosopher p : phils) { if (!p.eatten) { allEatten = false; break; } } System.out.print("");//It's here if (allEatten) { System.out.println("Everyone Eats"); break; } } catch (Exception e) { e.printStackTrace(System.out); } } System.out.println("Exit The Program!"); System.exit(0); }
Here my class Fork
static class Fork { public Semaphore sem = new Semaphore(1); void grab() { try { sem.acquire(); } catch (InterruptedException e) { e.printStackTrace(System.out); } } void release() { sem.release(); } }
And my class Philosopher
static class Philosopher implements Runnable { public boolean eatten = false; public Fork firstFork; public Fork secondFork; public Philosopher(Fork firstFork, Fork secondFork) { this.firstFork = firstFork; this.secondFork = secondFork; } @Override public void run() { try { while (true) { doAction(": Thinking"); firstFork.grab(); doAction(": Picked up the 1st fork"); secondFork.grab(); doAction(": Picked up the 2nd fork"); eatten = true; eat(); firstFork.release(); doAction(": Put down the 1st fork"); secondFork.release(); doAction(": Put down the 2nd fork. Back to thinking"); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); return;//đảm bảo thread dừng hẳn khi bị ngắt } } void eat() { try { int sleepTime = (int) (Math.random() * 1000); System.out.println(Thread.currentThread().getName() + " eats for " + sleepTime); Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(System.out); } } private void doAction(String action) throws InterruptedException { System.out.println(Thread.currentThread().getName() + " " + action); Thread.sleep(((int) (Math.random() * 100))); } }