whats wrong with this wait() Notify all ??
Hello again,
The program i had trouble with last time is as nearly as finished now but i cant seem to get it to work correctly. What i have is a main class App that creates 3Threads of Vrachtwagen and 2 Threads of Kraan, 1class ContainerShip and 1 class Kade, this Kraan is getting Containers of a ContainerShip class which creates 100 Container classes and the Kraan wants to put them on the Kade where a maximum of 5 containers can stay else the Kraan must wait for The Vrachtwagen class to pick them. The code where i think its going wrong is in the Kade class, in this class i got some methodes to set the Containers From the ContainerSchip to the Kade and a get method to get the Containers from the Kade again. So actually The real problem is that sometimes Vrachtwagen A, B and C want to get the same Container from the Kade which ofcourse should be impossible. I think its something in Kade but incase others were needed i posted all
code :
Code:
public class Kade {
Container GiveAway;
Container ContainersKade[] = new Container[5];
int KadeCount = 0;
boolean get;
public Kade() {
}
public synchronized Container GetContainer() {
get = false;
int x = 0;
while (!get) {
if (KadeCount > 0) {
if (ContainersKade[x] != null) {
get = true;
} else {
x++;
}}
else {
try {
wait();
} catch (InterruptedException e) {
}
}
}
GiveAway = ContainersKade[x];
ContainersKade[x] = null;
KadeCount--;
notifyAll();
return GiveAway;
}
public synchronized void SetContainer(Container x) {
int a = 0;
while (true) {
if (a == 5) {
try {
wait();
} catch (InterruptedException e) {
}
a = 0;
} else if (ContainersKade[a] == null) {
ContainersKade[a] = x;
notifyAll();
KadeCount++;
break;
} else {
a++;
}
}
}
}
Code:
public class Kraan extends Thread {
String Naam;
ContainerSchip C;
Kade K;
Container GET;
Random random=new Random();
public Kraan(ContainerSchip c, Kade k, String n) {
C = c;
K = k;
Naam = n;
}
public void run() {
while (true) {
if(C.Container[99]!=null){
GET = C.GetContainer();
try {
Thread.sleep(1000 + random.nextInt(5000));
} catch (InterruptedException ex) {
Logger.getLogger(Kraan.class.getName()).log(Level.SEVERE, null, ex);
}
K.SetContainer(GET);
System.out.println(Naam + " heeft container " + GET.Nummer + " Geplaatst op de Kade, er staan nu "+K.KadeCount+" containers op de kade.");
}
else{
C.leeg=true;
C.print();
break;
}
}
}
}
Code:
public class Vrachtwagen extends Thread {
ContainerSchip C;
Kade K;
String Naam;
Container GET;
Random random=new Random();
public Vrachtwagen(ContainerSchip c, Kade k, String s) {
C = c;
K = k;
Naam = s;
}
public void run() {
while (true) {
if (GET == null) {
GET = K.GetContainer();
System.out.println(Naam+" heeft container "+GET.Nummer+" ingeladen en nu staan er nog "+K.KadeCount+" containers op de kade.");
}
else if(K.KadeCount==0 && C.leeg){
System.out.println("De Kade is leeg, vrachtwagens hebben niks meer te doen.");
break;
}
else{
try {
Thread.sleep(1000+random.nextInt(4000));
} catch (InterruptedException ex) {
Logger.getLogger(Vrachtwagen.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(Naam+" heeft container "+GET.Nummer+" weggebracht.");
GET=null;
try {
Thread.sleep(1000+random.nextInt(4000));
} catch (InterruptedException ex) {
Logger.getLogger(Vrachtwagen.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(Naam+" is weer geariveerd op de kade.");
}
}
}
}
Code:
public class ContainerSchip {
Container Container[] = new Container[100];
Container GiveAway;
boolean geef;
boolean leeg=false;
public ContainerSchip() {
for (int i = 0; i < 100; i++) {
Container[i] = new Container();
Container[i].Nummer = i + 1;
}
}
public synchronized Container GetContainer() {
int x = 0;
geef = false;
while (!geef) {
if (Container[x] != null) {
geef = true;
} else {
x++;
}
}
GiveAway = Container[x];
Container[x] = null;
System.out.println("Schip geeft container "+GiveAway.Nummer);
return GiveAway;
}
public synchronized void print(){
if(leeg){
System.out.println("Schip is leeg");
}
}
}
Code:
public class Container {
int Nummer;
}
Code:
public class App {
ContainerSchip ContainerSchip = new ContainerSchip();
Kade Kade = new Kade();
Kraan Kraan1 = new Kraan(ContainerSchip, Kade, "Kraan 1");
Kraan Kraan2 = new Kraan(ContainerSchip, Kade, "Kraan 2");
Vrachtwagen WagenA = new Vrachtwagen(ContainerSchip, Kade, "Vrachtwagen A");
Vrachtwagen WagenB = new Vrachtwagen(ContainerSchip, Kade, "Vrachtwagen B");
Vrachtwagen WagenC = new Vrachtwagen(ContainerSchip, Kade, "Vrachtwagen C");
public static void main(String[] args) {
App App = new App();
App.run();
}
public void run() {
Kraan1.start();
Kraan2.start();
WagenA.start();
WagenB.start();
WagenC.start();
}
}
Sorry class names and variable names are Dutch as im Dutch :D
please help me out here ^^,
Kevin