rgrs2007 0 Newbie Poster

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

and have simplified the problem in the following manner:

SO: input pipe is PIPE4 and output pipe is PIPE0
S1: input pipe is PIPE0 and output pipe is PIPE1
S2: input pipe is PIPE1 and output pipe is PIPE2
S3: input pipe is PIPE2 and output pipe is PIPE3
S4: input pipe is PIPE3 and output pipe is PIPE4
The output that i am getting:

WHO CAME??
Pipe4 and Pipe0:Position0
GRANTING ACCESS TO
Pipe4 and Pipe0
WHO CAME??
Pipe0 and Pipe1:Position1
SLEEPING : 1
RELIEVING
NOTIFIED
Final Capacity at 0 is 4
4,4,3,1,2
*/
every time it gets stuck at this point.. can anyone help me with this..

Any help will be highly appreciated Thank you

I am enclosing the 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;

    //long thread_waiting;

    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 initial()
    {
        for (Thread th : Thread.getAllStackTraces().keySet()) 
        {  
            //if(th.getId()==thread_id)
            //{
                System.out.println("Thread: "+th.getId()+" in state: "+th.getState());
            //  th.destroy();
            //}
        }
    }

    public void checkAccess()
    {
        //boolean access_granted=false;

        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;
            initial();
            grantAccess(input_number,output_number);
        }
        else
        {
            synchronized(this)
            {
                //access_granted=false;
                try 
                {
                    System.out.println("SLEEPING : "+index);
                    initial();
                    wait();
                } 
                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());
            }
            finally
            {
                System.out.println("Final Capacity at "+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);
        relieveAccess(input_number,output_number);
    }

    //public void relieveAccess(int in_number,int out_number,long thread_id)
    public void relieveAccess(int in_number,int out_number)
    {
        System.out.println("RELIEVING");
        station_connection.setAvailability(true, in_number, out_number);
        synchronized(this)
        {
            notifyAll();
            System.out.println("NOTIFIED");
        }

        for (Thread th : Thread.getAllStackTraces().keySet()) 
        {  
            //if(th.getId()==thread_id)
            //{
                System.out.println("Thread: "+th.getId()+" in state: "+th.getState());
            //  th.destroy();
            //}
        }
    }
}

/*-----------------------------------------------------------------------------*/

public class PipeSystem
{
    ArrayList<Connection> pipe_connection=new ArrayList<Connection>();

    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].initial();
            st[i].checkAccess();
            st[i].t.start();
        }
        /*
        Runnable r=new PipeSystem();
        Thread thread=new Thread(r);
        thread.start();
        */
    }

    /*
    @Override
    public void run() 
    {
        System.out.println("RUNNING");
        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();
        }
    }
    */
}
}