次の質問の簡単なバージョンに取り組んでいます: 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;
}
}
/ -------------------------------------------------- ---------------------------- /
class 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();
}
}
*/
}
}
/ -------------------------------------------------- ---------------------------- /
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();
}
}
}