java - Netbeans, red text but no error -
i'm trying implement solution dinning philosophers. not sure if i'm doing right. programme isn't crashing getting reddish text in output, there's no error code.
example of error:
at (package_name).phil.getrightfork(phil.java:70) error alternatives between line 70, , 46 (which line calls getrightfork)
i've swapping getrightfork getleftfork, selects rightfork error
here's code i'm using: custom semaphore:
public class semaphore { public int value= 0 ; public semaphore(int value) { this.value = value; } public synchronized void up() { //notify must syncrhonized value++; if (value > 0){ this.notifyall(); } } public synchronized void down() throws interruptedexception { while (value <= 0){//check if resource avaiable, if not wait. this.wait(); } value--; // value no longer negative }}
main:
public class main { private static final int n = 10000; public static void main(string[] args) throws interruptedexception{ phil[] phils = new phil[n]; semaphore[] forks = new semaphore[n]; (int i=0;i<n;i++){ forks[i] = new semaphore(1); phils[i] = new phil(i, forks, n); phils[i].start(); } (int i=0;i<n;i++){ phils[i].join(); } }}
phil class:
public class phil extends thread { semaphore fork[]; int phil, total, left, right; boolean leftfork = false, rightfork = false; public phil(int spot ,semaphore[] s, int n){ phil = spot; left = spot; fork = s; switch(spot){ case 0: right = n-1; break; default: right = spot - 1; break; } } public void run(){ system.out.println("i phil " + phil + " left fork " + left + " right fork " + right); while(true){ seek { if (phil%2 == 0){ thread.sleep(10); // allow odd phils eat first } getrightfork(); if (rightfork){ getleftfork(); } if (leftfork && rightfork){ eat(); retleftfork(); retrightfork(); } thread.sleep(10); } grab (interruptedexception ex) { } } } void getleftfork() throws interruptedexception{ fork[left].down(); //system.out.println("i got left fork!"); leftfork = true; } void getrightfork() throws interruptedexception{ fork[right].down(); //system.out.println("i got right fork!"); rightfork = true; } void retleftfork(){ fork[left].up(); leftfork = false; } void retrightfork(){ fork[right].up(); rightfork = false; } void eat(){ system.out.println("phil:" + phil + " ate"); }}
you're getting nullpointerexception
. element in array trying access null
.
this caused fact start
phil
before entire array complete...
phil[] phils = new phil[n]; semaphore[] forks = new semaphore[n]; (int = 0; < n; i++) { forks[i] = new semaphore(1); phils[i] = new phil(i, forks, n); // phil starting, how many phils there?? phil.start(); }
instead, seek filling array first , starting them in separate loop...
phil[] phils = new phil[n]; semaphore[] forks = new semaphore[n]; (int = 0; < n; i++) { forks[i] = new semaphore(1); phils[i] = new phil(i, forks, n); } (phil phil : phils) { phil.start(); } (int = 0; < n; i++) { phils[i].join(); }
if solution requires phil
started created, need alter checking code handle situation next element may null
java netbeans dining-philosopher
No comments:
Post a Comment