1

I've got problems waking up Threads.

In my program there must be a moving Thread called Bus and several climbers who tries to enter the Bus to go somewhere else.

The Bus must be always in movement(except when is waiting for the climbers to get in/out) and the climbers must see if the Bus is in the correct place so they can get in/out.

So i made this code, but i can't get it to work, it seems like the Bus can't wake up the Threads but i can't came up with another solution and i don't know what's wrong.

¿How can i signal the climbers to wake up and get into the bus?

public class Bus extends Thread
{
boolean pointA=true, pointB=false;

public Bus() {
    start();
}

@Override
public void run()
{
    while(true)
    {
        waiting();
        goA();
        waiting();
        goB();
    }       
}        

public synchronized void waiting() {
    try {
        Thread.sleep((int)(1000));   //waiting for the climbers
    } catch (InterruptedException ex) {}
}

public synchronized void goB() {
    pointA = false;
    try {
        Thread.sleep((int)(1000));   //travelling
    } catch (InterruptedException ex) {}
    pointB = true;
    notifyAll();  //is in B, the climbers can get out
}

public synchronized void goA() {
    pointB = false;
    try {
        Thread.sleep((int)(1000));
    } catch (InterruptedException ex) {}
    pointA = true;
    notifyAll();  //is in A, the climbers can get in
} 

public synchronized void enter(Climber c, ArrayThreads fA, ArrayThreads fBus){

        while(!pointA){   //wait until Bus reaches pointA
            try{wait();}catch(InterruptedException e){}
        } 
        fA.out(c);     //Leaves station
        fBus.put(c);   //Enters the bus

        while(!pointB){    //wait until Bus reaches pointB
        try{wait();}catch(InterruptedException e){}
        }

        fBus.out(c);   //Leaves Bus

}

}
Anarion321
  • 25
  • 1
  • 4
  • Your class has compilation error: `public synchronized void wait()` "Cannot override the final method from Object". Please submit working code before asking. – Tagir Valeev May 03 '15 at 11:03
  • which one? i did not add imports, nor the main class or the climber class, maybe is that. The code works for me, but the problem is that the climbers remain waiting all the time. – Anarion321 May 03 '15 at 11:07
  • Even if you add proper imports and `Climber` class you still cannot redefine the `wait()` method as it's declared final in `Object`. Which version of JDK are you using? – Tagir Valeev May 03 '15 at 11:09
  • You were right, sorry, i change the name of the method by mistake while typing and copy pasting, it wasn't called wait(), now it should work like i've said. – Anarion321 May 03 '15 at 11:18
  • Ok, now it's better. And what is ArrayThreads type? There's no such type in JDK. – Tagir Valeev May 03 '15 at 11:29
  • It's a class i created myself, an array of Climbres which are printed in a textfield with the methods put and out public ArrayThreads(JTextField tf){ lista=new ArrayList(); this.tf=tf; } – Anarion321 May 03 '15 at 11:34

1 Answers1

0

The code that you have provided contains overrided wait method. But in Object class wait method is final. So one cannot override it.

No the problem in your code is that, you are calling wait and notifyall method from the same thread.

In you given code, you call synchronized method wait() and expect that other synchronized method goA() will notify, but that method will never be called. As wait method is holding lock.

So, modifying you program which have two thread, one for bus and one for creating climbers might help

Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
  • Sorry, i mistyped when i wrote the code, now is fixed, the Bus Thread never waits, only calls a method to put it to sleep, the only one that waits should be the Thread climber in the method Enter, and be waken up by the Bus Thread. – Anarion321 May 03 '15 at 11:25