0

我正在研究以下问题的更简单版本: http ://www.cs.ucf.edu/courses/cnt4714/spr2011/prog1.pdf

并以下列方式简化了问题:

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

我得到的输出:

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   
*/

每次它卡在这一点上..任何人都可以帮我解决这个..

任何帮助将不胜感激谢谢

我也附上代码:

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;
}

}

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

类 Station 实现 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());
        }
        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);       
}

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

}

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

公共类 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();
    }
}

}

4

0 回答 0