java - MultiThreading_problem with wait and notify -


i working on simpler version of following question: http://www.cs.ucf.edu/courses/cnt4714/spr2011/prog1.pdf

and have simplified problem in following manner:

so: input pipe pipe4 , output pipe pipe0 s1: input pipe pipe0 , output pipe pipe1 s2: input pipe pipe1 , output pipe pipe2 s3: input pipe pipe2 , output pipe pipe3 s4: input pipe pipe3 , output pipe pipe4 

the output getting:

who came?? pipe4 , pipe0:position0 granting access pipe4 , pipe0 came?? pipe0 , pipe1:position1 sleeping : 1 relieving notified final capacity @ 0 4 4,4,3,1,2    */ 

every time gets stuck @ point.. can me this..

any highly appreciated thank you

i enclosing code too:

package thread;  import java.util.arraylist;  class connection { string in_pipe; string out_pipe;  //true: available && false: occupied static boolean available[]={true,true,true,true,true};  public connection(string in_pipe,string out_pipe) {     this.in_pipe=in_pipe;     this.out_pipe=out_pipe; }  public void setavailability(boolean status,int in_pipe_number,int out_pipe_number) {     available[in_pipe_number]=status;     available[out_pipe_number]=status; }  public boolean getavailability(int pipe_number) {     return available[pipe_number]; }  public string getinpipe() {     return in_pipe; }  public string getoutpipe() {     return out_pipe; } 

}

/-----------------------------------------------------------------------------/

class station implements runnable { static int pump_capacity[]={5,4,3,1,2};

thread t; boolean flag_in=false; boolean flag_out=false; connection station_connection; int index;  int input_number,output_number;   public void setpumpcapacity(int capacity,int pipe_number) {     pump_capacity[pipe_number]=capacity; }  public int getpumpcapacity(int pipe_number) {     return pump_capacity[pipe_number]; }  public station(connection station_connection,int index) {     this.station_connection=station_connection;     this.index=index;     t=new thread(this); }  public void checkaccess() {     //boolean access_granted=false;     //long thread_waiting;      string input=station_connection.getinpipe();     input_number=integer.parseint(input.substring(input.length()-1));      string output=station_connection.getoutpipe();     output_number=integer.parseint(output.substring(output.length()-1));      system.out.println("who came??\n"+"pipe"+input_number+"\t\t"+"pipe"+output_number+"\t\tposition"+index);      if((station_connection.getavailability(input_number))&&(station_connection.getavailability(output_number)))     {         //access_granted=true;         grantaccess(input_number,output_number);     }     else     {         synchronized(this)         {             //access_granted=false;             try              {                 system.out.println("sleeping : "+index);                 wait();                 //thread_waiting=t.getid();             }              catch (interruptedexception e)              {                     e.printstacktrace();             }         }     } }  public void grantaccess(int input_number,int output_number) {     if(getpumpcapacity(index)>0)     {         system.out.println("granting access to\n"+"pipe"+input_number+"\t\t"+"pipe"+output_number);         station_connection.setavailability(false, input_number, output_number);     } }  @override public void run()  {     long id=t.getid();     if(getpumpcapacity(index)>0)     {         setpumpcapacity(getpumpcapacity(index)-1,index);         try          {             thread.sleep(5000);         }          catch (interruptedexception e)          {             system.out.println("catch exception : "+e.getmessage());         }                 {             system.out.println("final capacity @ "+index+" = "+getpumpcapacity(index));             for(int i=0;i<5;i++)             {                 system.out.print(pump_capacity[i]+"\t");             }             system.out.println();         }     }     relieveaccess(input_number,output_number,id);        }  public void relieveaccess(int in_number,int out_number,long thread_id) {     system.out.println("relieving");     station_connection.setavailability(true, in_number, out_number);     synchronized(this)     {         notify();         system.out.println("notified");     }     /*     (thread th : thread.getallstacktraces().keyset())      {           if(th.getid()==thread_id)         {             system.out.println("close");             th.destroy();         }     }     */ } 

}

/-----------------------------------------------------------------------------/

public class pipesystem { arraylist pipe_connection=new arraylist();

static connection connection[]={new connection("pipe4","pipe0"),     new connection("pipe0","pipe1"),     new connection("pipe1","pipe2"),     new connection("pipe2","pipe3"),     new connection("pipe3","pipe4"),     };  public void initialize() {     for(int i=0;i<5;i++)     {         pipe_connection.add(connection[i]);     } }  public static void main(string[] args)  {     pipesystem ob=new pipesystem();     ob.initialize();      station st[]=new station[5];     for(int i=0;i<5;i++)     {         st[i]=new station(connection[i],i);         st[i].checkaccess();         st[i].t.start();     } } 

}


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -