Results 1 to 10 of 10
  1. #1
    Ultre is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Question 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 :
    Java 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++;
    
                }
            }
        }
    }

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


    Java 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.");
                }
            }
        }
    }


    Java 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");
                
            }
           
        }
    }


    Java Code:
    public class Container {
    int Nummer;
    }


    Java 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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Sorry, I don't understand your problem. Can you add some debug code with println()s to show what is happening that you'd like to change? Then copy and paste the output here with comments describing what's wrong.

  3. #3
    Ultre is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    I translated the printouts to english so its easier to read, output:

    Ship gave container 1
    Ship gave container 2
    Truck C took container 2 there are now -2 containers on the wharf.
    Exception in thread "Thread-3" java.lang.NullPointerException
    Crane 2 has placed container 2 on the warf, there are now -2 containers on the wharf.
    Ship gave container 3
    at p3opdracht4remake.Vrachtwagen.run(Vrachtwagen.java :33)
    Exception in thread "Thread-2" java.lang.NullPointerException
    at p3opdracht4remake.Vrachtwagen.run(Vrachtwagen.java :33)

    Crane 2 has placed container 3 on the warf, there are now -1 containers on the wharf.
    Ship gave container 4
    Truck C has taken away 2 to it's destination
    Crane 1 has placed container 1 on the warf, there are now 0 containers on the wharf.
    Ship gave container 5
    Crane 2 has placed container 4 on the warf, there are now 1 containers on the wharf.
    Ship gave container 6
    Truck C has arrived on the wharf
    Truck C took container 3 there are now 0 containers on the wharf.
    Crane 2 has placed container 6 on the warf, there are now 1 containers on the wharf.
    Ship gave container 7
    Truck C has taken away 3 to it's destination
    Crane 1 has placed container 5 on the warf, there are now 2 containers on the wharf.
    Ship gave container 8

    So the problem is All 3 Trucks want to get the same Container at the same time, so only 1 is able to get it because afther that the container will be null so thats why the null-pinter exception comes in and then the other 2 Truck threads actually stop so then its just Truck C in this case, hope this makes you understand the problem better

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Looks like a logic problem. They can take a long time to figure.

    Truck C took container 2 there are now -2 containers on the wharf.
    This is interesting.

    Your print out doesn't show how many containers were on the wharf when Truck C arrived.
    If Truck C took one container(#2) then there must have been -1 containers on the wharf before. Assuming taking a container reduces the number of containers by one.

  5. #5
    Ultre is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    I'v changed the printlines so the wharf now constantly prints if Count is changed, here is some output with 3 trucks again:

    Ship gave container 1
    Ship gave container 2
    Crane 1 has placed container 1 on the warf.
    Containers on wharf: 1
    Ship gave container 3
    Truck C took container 1
    Containers on wharf: -2
    Exception in thread "Thread-3" Exception in thread "Thread-2" java.lang.NullPointerException
    at p3opdracht4remake.Vrachtwagen.run(Vrachtwagen.java :33)
    java.lang.NullPointerException
    at p3opdracht4remake.Vrachtwagen.run(Vrachtwagen.java :33)

    Crane 2 has placed container 2 on the warf.
    Containers on wharf: -1
    Ship gave container 4
    Truck C has taken away 1 to it's destination
    Crane 2 has placed container 4 on the warf.
    Containers on wharf: 0
    Ship gave container 5
    Truck C has arrived on the wharf
    Crane 1 has placed container 3 on the warf.
    Containers on wharf: 1
    Ship gave container 6
    Truck C took container 2
    Containers on wharf: 0
    Crane 1 has placed container 6 on the warf.
    Containers on wharf: 1
    Ship gave container 7
    Crane 2 has placed container 5 on the warf.
    Containers on wharf: 2
    Ship gave container 8
    Truck C has taken away 2 to it's destination

    But if i only do it with 1 truck then it seems to work:

    Ship gave container 1
    Ship gave container 2
    Crane 2 has placed container 2 on the warf.
    Containers on wharf: 1
    Ship gave container 3
    Truck A took container 2
    Containers on wharf: 0
    Crane 1 has placed container 1 on the warf.
    Containers on wharf: 1
    Ship gave container 4
    Crane 2 has placed container 3 on the warf.
    Containers on wharf: 2
    Ship gave container 5
    Truck A has taken away 2 to it's destination
    Crane 1 has placed container 4 on the warf.
    Containers on wharf: 3
    Ship gave container 6
    Crane 2 has placed container 5 on the warf.
    Containers on wharf: 4
    Ship gave container 7
    Truck A has arrived on the wharf
    Truck A took container 1
    Containers on wharf: 3
    Truck A has taken away 1 to it's destination
    Crane 2 has placed container 7 on the warf.
    Containers on wharf: 4
    Ship gave container 8
    Crane 1 has placed container 6 on the warf.
    Containers on wharf: 5
    Ship gave container 9

    so im wondering why dont those Crane threads mess up but only the Trucks, they are trying to grab the same container :confused:

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Containers on wharf: 1
    Ship gave container 3
    Truck C took container 1
    Containers on wharf: -2
    Looks like a logic error here.
    If there is 1 on wharf and a ship adds one there would be 2
    Then if a truck takes 1 there should be 1 left. But the output shows -2 ???

  7. #7
    Ultre is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    -2 is because 3 Trucks are trying to pick that container so truck A tries it then there is 0 but then B goes aswell and then there is -1 and then C goes and then there is -2 but only 1 truck should go because its a synchronized method and it checks if there is a container else it waits. because of if(KadeCount>0) and KadeCount is the output like 0,-1,-2 so basicly how can Truck B go when Truck A puts the Count back on 0:confused:

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    A truck should NOT be able to get a container if there are none available.
    When the waiting truck is awaken from wait, it should test if there are any containers available, if none go back to waiting.
    psuedo code:
    Java Code:
    private synchronized getContainer() {
      while(nbr containers available == 0) {
         wait();
      }
      .. get the container
    }

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    The wharf is the critical resource here: it can contain <= 5 containers and when you want to put more on it you have to wait; you also have to wait when you want to get a container and none are available.

    Implement put(Container c) and getContainer() in the Wharf class and make those methods synchronized; when those methods have succesfully put or taken a container they notifyAll() threads waiting on that critical resource.

    kind regards,

    Jos

  10. #10
    Ultre is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    thanks iv got this working now by adding a boolean available ^^

Similar Threads

  1. Need help with wait() and notify()
    By Mkaveli in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-30-2010, 11:58 AM
  2. Wait() notify() implementation
    By georges in forum Advanced Java
    Replies: 4
    Last Post: 02-05-2010, 01:33 AM
  3. wait() and notify() trouble with UI
    By Atriamax in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-09-2009, 02:51 AM
  4. wait() and notify() problems
    By greyradio in forum Threads and Synchronization
    Replies: 1
    Last Post: 08-03-2009, 03:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •