Can’t relase the lock with notify

I’ve got a test on java thread coding but i have some basic problem.. after hours of trying and searching I decided to try there!

I can’t understand why my wait is still locked even after my notify :

Here you can find my code:

public class Mymain {

    public static void main(String[] args) {

        for( int i=0;i<100;i++){
            new ThreadClass(i).start();     
        }
    }
}

public class ThreadClass extends Thread {
    static boolean ok = false;
    int id;

    public ThreadClass(int i) {
        id = i;
    }

    public void run() {
        System.out.println("Thread start " + id);
        Last.toDo(id);
        if (id == 5)
            try {
                waiting();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        if (id != 5)
            awaking();

        System.out.println("thread end " + id);
    }

    private synchronized void awaking() {
        // TODO Auto-generated method stub
        if (ok) {
            System.out.println("i'm " + id + " and i'm Awaking 5");
            ok = false;
            notify();
            System.out.println("I did the notify and i'm " + id);
        }
    }

    private synchronized void waiting() throws InterruptedException {
        System.out.println("Sleeping");
        ok = true;
        wait();
        System.out.println("Awake 5");
    }
}

Result

Then it starts looping or it goes in dead lock not sure.. it should just stop the thread with id=5 and then the next thread should re-start the id = 5.. but thread 5 never wake up after the notify…

In the result as you can see I got 2 threads trying to wake up the thread 5 and thread 5 is always waiting since the start^^

Answer

Look I did a few changes to your code:

  1. You can’t just notify() , you’ll notify to this. And you can’t just wait() , you’ll wait forever. You have to use these function over an Object, so i added an Integer object (just to show you – you’ll have to choose the right object).
  2. You have yo understand between synchronized and static synchronized. A quick search would lead you to a perfect answer.
  3. Why function waiting() is synchronized? only thread number 5 calls it.
  4. When calling to an Object.notify() / Object.wait(), you have to declare a synchronized block over the object.

Here’s some code:

public class Threads {
    public static void main(String[] args) {
        Integer intObject = new Integer(0);
        for( int i=0;i<100;i++){

            new ThreadClass(i, intObject).start();     
        }
    }
}
class ThreadClass extends Thread {
    static boolean ok = false;
    int id;
    Integer intObject;
    public ThreadClass(int i, Integer intObject) {
        id = i;
        this.intObject = intObject;
    }

    public void run() {
        System.out.println("Thread start " + id);
        //Last.toDo(id);
        if (id == 5)
            waiting();
        else
            awaking(this);

        System.out.println("thread end " + id);
    }

    private static synchronized void awaking(ThreadClass t) {
        if(ok) {
            System.out.println("i'm " + t.id + " and i'm Awaking 5");
            ok = false;
            synchronized (t.intObject) {
                    t.intObject.notify();
                }
            System.out.println("I did the notify and i'm " + t.id);
        }
    }
    private void waiting(){
        System.out.println("Sleeping");
        ok = true;
        synchronized (intObject) {
            try {
                intObject.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Awake 5");
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *