Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7697201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:57:21+00:00 2026-05-31T21:57:21+00:00

I read that: Object sync = new Object(); /* inter to monitor */ try

  • 0

I read that:

Object sync = new Object();

/* inter to monitor */
try {
    sync.wait();
} catch (InterruptedException ex) {

} finally {

}

/* our code*/
/* exit from monotir */
sync.notify();

is like

synchronize (sync) {
    /* our code*/
}

Does it true?

I try this in my code and it doesn’t work.

private Object data;
    private Object sync = new Object();

    public static void main(String[] args) {
        SimpleProducerConsumer pc = new SimpleProducerConsumer();
        new Thread(pc.new Producer()).start();
        new Thread(pc.new Consumer()).start();
    }

    public Object pop() {
        try {
            sync.wait();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        System.out.println("Before");
        Object d = data;
        data = null;
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("pop");
        System.out.println("After");
        sync.notify();
        return d;
    }

    public void push(Object d) {
        try {
            sync.wait();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        data = d;
        System.out.println("push");
        sync.notify();
    }

    class Producer implements Runnable {
        @Override
        public void run() {
            while (true) {
                push(new Object());
            }
        }
    }

    class Consumer implements Runnable {
        @Override
        public void run() {
            while (true) {
                pop();
            }
        }
    }

What is wrong with my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:57:23+00:00Added an answer on May 31, 2026 at 9:57 pm

    No, sync.wait() and sync.notify() are far from synonyms to sychronize (sync) { ... and ... }.

    In fact, in order to call wait you have to be in a synchronized block.

    I’ll explain the concepts of synchronize, wait and notify and the difference will be clear.

    • When a thread enters a synchronized (sync) { ... } block it grabs the lock of the sync monitor (meaning that the lock is taken and that no other thread can enter the block at that point).

    • When a thread calls sync.wait() it temporarily releases the lock of sync. (It’s waiting for something to happen. Something that requires the lock to be able to happen.)

    • A thread calls sync.notify() to notify other threads that something has happened, at which point they resume execution.

    What is wrong with my code?

    I assume you for instance want to make sure that if two threads try to pop something they should avoid doing the following:

    Thread 1              Thread 2
    --------------------------------------
    Object d = data;
                          Object d = data;
                          data = null;
    data = null;
    return d;
                          return d;
    

    To avoid this you need to make sure that d = data and data = null happens atomically.

    This can be achieved by doing

    Object d;
    synchronized (sync) {
        d = data;
        data = null;
    }
    

    (Now the first and second statement in Thread 1 above can’t be split up.)

    Furthermore you seem to want pop to be blocking, i.e. if d == null it should wait for some other thread to push something.

    This can be achieved by doing

    synchronized (sync) {
        while (d == null)
            sync.wait();
    }
    

    and do sync.notify() in push (again within a synchronized block covering everything that needs to be done atomically in the push method).

    Related question / further reading:

    • The Java™ Tutorials: Guarded Blocks
    • Why must wait() always be in synchronized block

      My answer over here gives an example of what could happens in a producer / consumer situation if wait and notify where allowed to be called outside a synchronized block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read that using Object.prototype to attach functions to custom JavaScript objects is more
I would like to know if PHP is Object Oriented, I read that from
I think I read somewhere that some modules only have object oriented interfaces (
I read here about std::auto_ptr<>::operator= Notice however that the left-hand side object is not
I've often read that in the Sun JVM short-lived objects (relatively new objects) can
I have read that dealloc for an object will be called, only if retain
I read that every object in java have a lock.That will be acquired by
I've read that it is usually bad practice to extend System.Object, which I do
I read that node.js is single-threaded, so it doesn't fork a new process or
So I've read that you're supposed to access object attributes through getter/setter methods like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.