0

I've a problem with my code. I'm trying to execute two simple GUI frames (just for test purposes) in two threads. I want to wait for a thread using wait() and to notify using notify() or notifyAll() the second thread. I've also used synchronized() and did not work.

Can anyone explain to me very clear (with an example) what I'm doing wrong ? I'm new to programming and I don't know much about it. Thanks !

Here's my snippet:

public void example5() throws InterruptedException {

    Thread t = new Thread() {
    public final Object obj = new Object();
    public void run() {
          synchronized(this) {   //coordinating activities and data access among multiple threads
                                //The mechanism that Java uses to support synchronization is the monitor
               try {
                obj.wait(3000); //suspendarea thread-ului t / punerea in asteptare

            } catch (InterruptedException ex) {

            }
        JFrame frame  = new JFrame();
        JButton b = new JButton("CLICK ME 1");
        JPanel panel = new JPanel();
        panel.add(b); frame.add(panel); frame.setBounds(700, 500, 150, 100); frame.setVisible(true);
          }
    }
};

        }

Thread t1 = new Thread(new Runnable() {

    public final Object obj = new Object();

        public void run() {
       obj.notify();

        JFrame frame  = new JFrame();
        JButton b = new JButton("CLICK ME 2");
        JPanel panel = new JPanel();
        panel.add(b); frame.add(panel); frame.setBounds(700, 500, 150, 100); frame.setVisible(true);
    }
});
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • I forgot one thing....i've also received an error : IllegalMonitorStateException , i've done some reasearch over the internet but i did not get it why this occurs.... – Ionutz Asaftei Aug 18 '15 at 20:01
  • 2
    You really rarely actually need to use wait and notify directly; if you don't understand them, I'd advise looking for other solutions. Check out the excellent higher level concurrency classes in the Java.util.concurrent package. – Andy Turner Aug 18 '15 at 20:04
  • 1
    You need to synchronize to notify. See http://stackoverflow.com/questions/886722/how-to-use-wait-and-notify-in-java. – Don Branson Aug 18 '15 at 20:05
  • Have a look at this article for better clarity on wait() and notify() concepts : http://www.journaldev.com/1037/java-thread-wait-notify-and-notifyall-example – Ravindra babu Aug 18 '15 at 20:06

1 Answers1

1

wait and notify should be called on the same object. Try this:

        Object ob1 = new Object();

    Thread t1 = new Thread() {
        @Override
        public void run() {
            try {
                System.out.println("ob1 tries to wait in t1...");
                synchronized (ob1) {
                    ob1.wait();
                }
                System.out.println("ob1 is up in t1!");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    Thread t2 = new Thread() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);

                System.out.println("t2 tries to notify ob1's single waiter...");
                synchronized (ob1) {
                    ob1.notify();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t2 notified ob1's single waiter!");
        }
    };

    t1.start();
    t2.start();

output:

ob1 tries to wait in t1... t2 tries to notify ob1's single waiter... t2 notified ob1's single waiter! ob1 is up in t1!

mtuulu
  • 1,042
  • 1
  • 10
  • 14
  • Thanks a lot guys ! You really helped me. Perfect example...easy and simple to understand. Now i know what i've done wrong...again, thanks. – Ionutz Asaftei Aug 19 '15 at 18:45