Q:

A simple wait() & notify() example - doesn't work

I tries to write a simple example that demonstrates a remote controller opertation.
The input is being accepted via the console, and a message is being printed accordingly.
Both threads run infinite loops: the main thread waits for notification, while the other waits for a console input.
I'd like to know how to fix it. The problem is that the notify doesn't stop the waiting: in other words, the words "before wait" are printed, but the words "after wait" are not. BTW, without the while(true) loops it works fine (for one button press).
Many thanks
    public class MainTV {
    public static int lastRemoteCode = 0;   
    public static void main(String[] args){
        RemoteControllerThread remote = new RemoteControllerThread();
        remote.start();
        synchronized(remote){
            while(true){                    
                try {
                    System.out.println("before wait");
                    remote.wait();
                    System.out.println("after wait");
                    switch (lastRemoteCode){ //we use switch because there are many code options
                    case 0:
                        System.out.println("Error with remote button reading");
                        break;
                    case 3:
                        System.out.println("Volume Down button was pressed now!");
                        break;
                    case 4:
                        System.out.println("Volume Up button was pressed now!");
                        break;
                    }
                } catch (InterruptedException e) {                  
                    e.printStackTrace();
                }               

            }
        }
    }
}

and the second class (simulates the remote controller):
    import java.util.Scanner;
    public class RemoteControllerThread extends Thread{
    public void run(){
        synchronized(this){
            Scanner in = new Scanner(System.in); 
            while(true){
                System.out.println("Press a button in the remote please...");
                int code = in.nextInt();                
                MainTV.lastRemoteCode = code;
                System.out.println("before notify");
                notify();   
                System.out.println("after notify");
            }
        }
    }
}

A:

Both threads synchronize on the same object, this refers to the same object as remote and since both this synchronize blocks have infinite loops inside, this creates a problem. One of the thread will wait for the other one to finish, which never happens (because of the infinite loops).
To fix this, you should synchronize only the code that needs to be synchronized, like the wait() and notify() calls.

