Вы находитесь на странице: 1из 4

import java.awt.

*;
import java.lang.Math;
import java.lang.Thread;

public class Lines


{
public boolean[] lineStatus = new boolean[5];

public synchronized void setRed (int lineNo)


{
lineStatus[lineNo]=true; //true means 'under use'
}

public synchronized void setGreen (int lineNo)


{
lineStatus[lineNo]=false;
}

// the following method is never called (for the moment)


public synchronized void waitGreen(int lineNo) // wait until a green
light appears at lineNo
{
if(lineStatus[lineNo]=true)
{
System.out.println("Line " + lineNo+ " is available, NO
NEED TO WAIT");
//return;
}
else{
System.out.println("Line " + lineNo+ " is not
available!!!!! You have TO WAIT");
}
}

import java.awt.*;
import java.lang.Math;
import java.lang.Thread;

public class Train2 extends Thread


{
public String trainName;
public float speed;
public int distanceRun=0;
public float timeRun=0;
public int distanceMax=0;
public Lines line;
public int startStation;
public int currStation;

public Train2(String aName, Lines theLine,int stStation, int finalDistance,int


currentStation )
{
trainName= aName;
line= theLine;
startStation= stStation;
distanceMax= finalDistance;
speed=(float)Math.random()*180.0f;
currStation=currentStation;
}

public void run()


{
System.out.println(trainName + " begins from Station no " +
startStation + " with travelling " + distanceRun + "km.");

while(distanceRun < distanceMax)


{
try{
sleep(2000);
}
catch(InterruptedException e) {}

if( startStation<2)
{

System.out.println(trainName + " attempts to use


line " + currStation);
line.waitGreen(currStation);

System.out.println("the line no " + currStation + "


is free? " + line.lineStatus[currStation]);

if(line.lineStatus[currStation]= true)
{
//line.setRed(currStation);
System.out.println(trainName + " can pass
from line " + currStation);

distanceRun= distanceRun + 25;


try{
sleep(
(int)(distanceRun/speed)*1000);
}
catch(InterruptedException e) {}

line.setGreen(currStation);
currStation= currStation+ 1;
System.out.println("ouaou " + trainName + "
has arrived at Station " + currStation + " add has made " + distanceRun + "km");
}
else
{ System.out.println("what the fuck");
try{
sleep(5000);
}
catch(InterruptedException e) {}
}
}
}
System.out.println(trainName + " has arrived at last at his
lastStation witch is " + currStation + " and has made " + distanceRun + " \n");
}

import java.awt.*;
import java.lang.Math;
import java.lang.Thread;

public class TrainTest2


{

public static void main(String[] args)


{
Lines line = new Lines();
line.lineStatus[0]= false;
line.lineStatus[1]= false;
line.lineStatus[2]= false;
line.lineStatus[3]= false;

Train2 trA = new Train2("Train A", line, 0, 100, 0); //String aName, Lines
theLine,int stStation, int finalDistance
Train2 trB = new Train2("Train B", line, 1, 75, 1);
Train3 trC = new Train3("Train C", line, 4, 100, 4);
Train3 trD = new Train3("Train D", line, 3, 50, 3);

System.out.println(trA.trainName + " starts at " + trA.currStation + " and has


speed "+ trA.speed + " km/h");
System.out.println(trB.trainName + " starts at " + trB.currStation + " and has
speed "+ trB.speed + " km/h");
System.out.println(trC.trainName + " starts at " + trC.currStation + " and has
speed "+ trC.speed + " km/h");
System.out.println(trD.trainName + " starts at " + trD.currStation + " and has
speed "+ trD.speed + " km/h");

trA.start();
trB.start();
trC.start();
trD.start();
}
}

import java.awt.*;
import java.lang.Math;
import java.lang.Thread;

public class Train3 extends Thread


{
public String trainName;
public float speed;
public int distanceRun=0;
public float timeRun=0;
public int distanceMax=0;
public Lines line;
public int startStation;
public int currStation;

public Train3(String aName, Lines theLine,int stStation, int finalDistance,int


currentStation )
{
trainName= aName;
line= theLine;
startStation= stStation;
distanceMax= finalDistance;
speed=(float)Math.random()*180.0f;
currStation=currentStation;
}

public void run()


{
System.out.println(trainName + " begins from Station no " +
startStation + " with travelling " + distanceRun + "km.");
while(distanceRun < distanceMax)
{
try{
sleep(2000);
}
catch(InterruptedException e) {}

System.out.println(trainName + " attempts to use


line " + (currStation-1));
line.waitGreen(currStation-1);

System.out.println("the line no " + currStation + "


is free? " + line.lineStatus[currStation]);

if(line.lineStatus[currStation-1]= true)
{
//line.setRed(currStation-1);
System.out.println(trainName + " can pass
from line " + (currStation-1));
distanceRun= distanceRun + 25;
timeRun= (float)(timeRun+
(distanceRun/speed)*60 + 5);
try{
sleep(
(int)(distanceRun/speed)*1000);
}
catch(InterruptedException e) {}
line.setGreen(currStation-1);
currStation= currStation- 1;
System.out.println("ouaou " +
trainName + " has arrived at Station " + (currStation) + " add has made " + distanceRun
+ "km and time " + timeRun + " min\n");
}
else
{
try{
sleep(5000);
}
catch(InterruptedException e) {}
}

}
System.out.println(trainName + " has arrived at last at his
lastStation witch is " + currStation + " and has made " + distanceRun+ " km and time " +
timeRun + "min\n");
}
}