I want to make a plane (Thread) fly from one airfield to another. When a plane is starting the airfield is blocked for 5 sec. (if another plane wants to land or take off - must wait). If a plane reach its destination it lands (if airfield is not blocked by another plane) and then takes a random airfield and fly there and so on.... I have questions in comments - how to make a thread wait ? And what else is wrong with my code ? Here is my class Samolot aka Plane:
public class Samolot extends Thread{
int id;
double paliwo;
Lotnisko source; //Lotnisko aka airfield
Lotnisko dest;
double xPosition;
double yPosition;
double xTarget;
double yTarget;
public Samolot(Lotnisko source, Lotnisko dest) {
this.source = source;
this.dest = dest;
paliwo = 100;
}
public void run(){
while(true){
tryStart();
}
}
public void tryStart(){
if(source.pas == true){ // if true the airfield is not blocked and we can go
source.timer(); // its a method in class Lotnisko, makes pas = false for 8sec
lecimy(source, dest);
}
else if(source.pas == false){
// how to make a thread wait ?
}
}
public void tryLadowanie(){
if(dest.pas == true){
dest.timer();
source = dest;
dest = Rand(source);
tryStart();
}
else if(dest.pas == false){
//how to make a thread wait ?
}
}
public void lecimy(Lotnisko source, Lotnisko dest){
xPosition = source.coords.getX();
yPosition = source.coords.getY();
xTarget = dest.coords.getX();
yTarget = dest.coords.getY();
while( (xPosition != xTarget) && (yPosition != yTarget) ){
update();
try{
sleep(100);// ok
}
catch (InterruptedException e) {
System.out.println("Error");
}
}
tryLadowanie();
}
public void update() {
paliwo -= 0.05;
double dx = xTarget - xPosition;
double dy = yTarget - yPosition;
double length = sqrt(dx*dx+dy*dy);
dx /= length;
dy /= length;
if (Math.abs(dest.coords.getX() - source.coords.getX()) < 1)
dx = 0;
if (Math.abs(dest.coords.getY() - source.coords.getY()) < 1)
dy = 0;
xPosition += dx;
yPosition += dy;
}
public Point getPositions() {
Point curPos = new Point((int)xPosition, (int)yPosition);
return curPos;
}