Calling Java wait () object interrupts thread synchronization

public class Main2 {
    public static void main(String[] args) {
        new Test2().start();
        new Test2().start();
    }
}

class Test2 extends Thread {
    @Override
    synchronized public void run() {
        try {
            System.out.println("begin wait");
            wait();
        } catch (Exception ex) {
        }
    }
}

As the actual result of running the test: start waiting, start waiting, two times from two threads. Compare with the expected result: start waiting, only once from one of the two threads, because wait () is called inside the synchronized run () method. Why can I call the Waiting for Object function () to interrupt thread synchronization?

Too much!


    public class Main3 {

    public static void main(String[] args) {
        Test3 t = new Test3();
        new Thread(t).start();
        new Thread(t).start();
    }
}

class Test3 implements Runnable {
    synchronized public void run() {
        try {
            System.out.println("begin wait");
            wait();
        } catch (Exception ex) {
        }
    }
}

@akf and @Sean Owen

Thank you for your responses. Sorry for my mistake, now I changed the code to put the synchronization in the same run () object, the result is: begin wait, begin wait, two times.

@akf

wait will release the lock that the synchronization has seized and will be re-received after the stream is notified.

Could you sort it out a bit?

+5
4
  • , , , , Test2 .
  • , , sleep, wait. wait , synchronized, .

, . wait , , "". , , , .

public static void main(String[] args)
{
    final Object lock = new Object(); 

    final JFrame frame = new JFrame("Notify Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Notify");
    button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt) {
            synchronized(lock) {
                lock.notify();
            }
        }
    });
    frame.add(button);

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            frame.setVisible( true );
        }
    });

    new Thread(new Runnable() {
        public void run() {
            synchronized(lock) {
                try {
                    System.out.println("1. starting");
                    lock.wait();
                    System.out.println("1. step 1");
                    lock.wait();
                    System.out.println("1. step 2");
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
            }
        }
    }).start();
    new Thread(new Runnable() {
        public void run() {
            synchronized(lock) {
                try {
                    System.out.println("2. starting");
                    lock.wait();
                    System.out.println("2. step 1");
                    lock.wait();
                    System.out.println("2. step 2");
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
            }
        }
    }).start();

}

wait JavaDoc :

, notify() notifyAll() . , , (0).

. , , , , notifyAll. , .

+10

Test2. . , , .

+2

, , :

        public class test {
            public static void main(String[] args) {
                Prova a=new Prova();
                new Test2(a).start();
                new Test2(a).start();
            }
        }
        class Prova{
            private boolean condition;

            public void f(){

                while(condition){
                    //Thread.currentThread  Returns a reference to the currently executing thread object.
                    //Thread.getName() return name Thread
                    System.out.println(Thread.currentThread().getName()+" begin wait");
                    try{
                        wait();
                    }catch(InterruptedException c){return;}
                }       

                System.out.println(Thread.currentThread().getName()+" first to take the mutex");
                condition=true;

            }
        }
        class Test2 extends Thread {
            private Prova a;
            private static boolean condition;


            public Test2(Prova a){
                this.a=a;
            }
            @Override

             public void run() {
               synchronized(a){
                try {           
                    a.f();           
                } catch (Exception ex) {
                }
               }
            }
        }

, , .

+1

/:

1) , , wait(), ( ), wait().

2), wait(), , , , , .

JavaDoc:

:

• .

• .

• Class, .

3) , /notifyAll (),, , /notifyAll (). wait(), .

4) /notifyAll () , , .

:

    public class Main3 {

    public static void main(String[] args) {
        Test3 t = new Test3();
        new Thread(t).start();
        new Thread(t).start();
        try {
            Thread.sleep(1000);
        } catch (Exception ex) {
        }
        t.testNotifyAll();
    }
}

class Test3 implements Runnable {

    synchronized public void run() {

        System.out.println(Thread.currentThread().getName() + ": " + "wait block got the lock");
        try {
            wait();
        } catch (Exception ex) {
        }
        System.out.println(Thread.currentThread().getName() + ": " + "wait block got the lock again");
        try {
            Thread.sleep(1000);
        } catch (Exception ex) {
        }
        System.out.println(Thread.currentThread().getName() + ": " + "bye wait block");

    }

    synchronized void testNotifyAll() {
        System.out.println(Thread.currentThread().getName() + ": " + "notify block got the lock");
        notifyAll();
        System.out.println(Thread.currentThread().getName() + ": " + "notify sent");
        try {
            Thread.sleep(2000);
        } catch (Exception ex) {
        }
        System.out.println(Thread.currentThread().getName() + ": " + "bye notify block");
    }
}

:

Thread-0 ( 1):

Thread-1 ( 0):

main:

main:

main: bye notify block

Thread-0 ( 1):

Thread-0 ( 1): bye

Thread-1 (or 0): the wait block received a lock again

Thread-1 (or 0): bye wait block

-1
source

All Articles