java - Dinning philosophers algorithm deadlock -



java - Dinning philosophers algorithm deadlock -

i have written algorithm solved dinning philosophers problem. giving stackoverflow exception.can 1 help me same. on issue. dinning philosopher problem using multi threading. using arraylist store chopsticks , plates.

` -----------

package com.intech.diningphilosopherproblem; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.timeunit; import javax.annotation.processing.processor; public class myphilosophers { /** * @param args */ static int noofp = ((runtime.getruntime().availableprocessors() * 2) + (1)); // runtime.getruntime().availableprocessors(); public static void main(string[] args) throws interruptedexception { myphilosopherschopstick myps = new myphilosopherschopstick(noofp); executorservice execserv = executors.newfixedthreadpool(5); (int = 0; < 500; i++) { thread.sleep(200); execserv.execute(myps); } // execserv.shutdown(); // disable new tasks beingness submitted seek { // wait while existing tasks terminate if (!execserv.awaittermination(600, timeunit.seconds)) { execserv.shutdownnow(); // cancel executing tasks // wait while tasks respond beingness cancelled if (!execserv.awaittermination(600, timeunit.seconds)) system.err.println("pool did not terminate"); } } grab (interruptedexception ie) { // (re-)cancel if current thread interrupted execserv.shutdownnow(); // preserve interrupt status thread.currentthread().interrupt(); } } }

`

bundle com.intech.diningphilosopherproblem; public class myphilosopherschopstick implements runnable { volatile int eatingturns =0; volatile int allowedeatingturns =200; volatile int noofchopsticks=0; volatile int[] arraychopsticks; volatile int i=0; public myphilosopherschopstick(int chopsticks) { arraychopsticks=new int[chopsticks]; this.noofchopsticks=chopsticks; (int = 0; < noofchopsticks; i++) { arraychopsticks[i]=0; } system.out.println("noofchopsticks"+noofchopsticks); system.out.println("arraychopsticks.length"+arraychopsticks.length); } public void run() { system.out.println("run count"+ i++); while(eatingturns<allowedeatingturns) { string s=thread.currentthread().getname(); char c=s.charat(s.length()-1); //c = thread thread no 1, thread no 2, thread no 3, thread no 4. denotes philosopher in char int j=(int)c; //j = thread thread no 1, thread no 2, thread no 3, thread no 4. denotes philosopher in int 0=49 , 1=50.... , on system.out.println("j"+j); if(c>noofchopsticks) system.out.println("c"+c); j=j-49; //converting char in int digits value 0=49, 1=50.... , on system.out.println("j"+j); system.out.println( thread.currentthread().getname()+"thinking-------------"+j); seek { thread.sleep(500); } grab (interruptedexception e) { // todo auto-generated grab block e.printstacktrace(); } hungary(j); } } public void hungary(int i) { //system.out.println( thread.currentthread().getname()+"inside hungary()-------------"+i); //else nil boolean a=eat(i); if(a==true) system.out.println(thread.currentthread().getname()+"eating-------------"+i+""+arraychopsticks[i]); synchronized (this){ eatingturns++; } a=doneeating(i); } private boolean eat(int i) { // todo auto-generated method stub // system.out.println( // thread.currentthread().getname()+"inside eat()-------------"+i); boolean a; boolean b; if (i == 0) { synchronized (this) { if ((a = pickchopstickright(i)) && (b = pickchopstickleft(arraychopsticks.length - 1))) { homecoming true; } keepchopstickright(i); // if right chopstick not available homecoming false; } } else { if ((a = pickchopstickright(i)) && (b = pickchopstickleft(i - 1))) { homecoming true; } keepchopstickright(i); // if right chopstick not available homecoming false; } } private boolean doneeating(int i) { // todo auto-generated method stub //system.out.println( thread.currentthread().getname()+"inside wanttothink()-------------"+i); boolean a; boolean b; if(i==0){ //synchronized (this) { a=keepchopstickleft(arraychopsticks.length-1); b=keepchopstickright(i); } } else { a=keepchopstickleft(i-1); b=keepchopstickright(i); //pandey } if(a&&b) { homecoming true; } homecoming false; } private boolean keepchopstickleft(int i) { //system.out.println( thread.currentthread().getname()+"inside keepchopstick left -------------"+i); if(arraychopsticks[i]==1) { system.out.println( thread.currentthread().getname()+"keeping downwards left chopstick -------------"+i+""+arraychopsticks[i]); arraychopsticks[i]=0; homecoming true; } homecoming false; } private boolean keepchopstickright(int i) { system.out.println( thread.currentthread().getname()+"inside keepchopstick right -------------"+i+""+arraychopsticks[i]); if(arraychopsticks[i]==1) { system.out.println( thread.currentthread().getname()+"keeping downwards right chopstick -------------"+i+""+arraychopsticks[i]); arraychopsticks[i]=0; run(); //goest run } homecoming false; } private boolean pickchopstickleft(int i) { //system.out.println( thread.currentthread().getname()+"inside pickchopstickleft -------------"+i); if(arraychopsticks[i]==0) { system.out.println( thread.currentthread().getname()+"picking chopstick left -------------"+i+""+arraychopsticks[i]); arraychopsticks[i]=1; homecoming true; } homecoming false; } private boolean pickchopstickright(int i) { //system.out.println( thread.currentthread().getname()+"inside pickchopstickright -------------"+i); if(arraychopsticks[i]==0) { system.out.println( thread.currentthread().getname()+"picking chopstick right -------------"+i+""+arraychopsticks[i]); arraychopsticks[i]=1; homecoming true; } homecoming false; } }

it might beauase of there memory leak in programme or have increment heap , stack size using -xmx -xms

java multithreading

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -