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 3242518
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:19:29+00:00 2026-05-17T18:19:29+00:00

I am trying to solve the readers-writers problem with writer preference in Java using

  • 0

I am trying to solve the readers-writers problem with writer preference in Java using multi-threading. The following is a stripped down version of what my code does. Will it work?

public PriorityBlockingQueue<myClass> pq;
public void foo(){
    myClass obj = new myClass();
    pq.add(obj);
    obj.wait();
    //Actual code
}
public void bar(){
    pq.remove().notify();
}

Assume that the priority queue pq is empty initially and the constructor of the enclosing class calls the constructor of pq. Also, foo is called first by one thread and then bar by another thread. So when foo is called, it adds obj to the queue and that becomes the front element so that when the remove is called in bar that is the element that is removed. My question is, will “Actual code” be executed? Or am I performing wait() and notify() on two completely different objects? If so, how can I fix it?

  • 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-17T18:19:30+00:00Added an answer on May 17, 2026 at 6:19 pm

    The main issue I can see with this is that threads can wake up spuriously… so you should always have some data associated with conditions. Also notifyAll() is less likely to result in deadlock… so:

    public void foo() {
         MyClass obj = new MyClass();
         pq.add(obj);
         synchronized(obj) {
             while (!obj.isDoneBeingProcessed()) {
                 obj.wait();
             }
          }
     }
    
     public void bar() {
          MyClass next = pq.remove();
          if (!next) {
              return;
          }
          next.doProcessing();
          synchronized(next) {
              next.setDone(true);
              next.notifyAll();
          }
     }
    

    Note, though, that this code really doesn’t make sense because it essentially serializes the entire computation. It would make more sense if you were to enqueue everything in one thread, while in another thread you did the processing, and then … only at the end or in another thread attempted to wait on everything. Putting the wait in the producer phase before everything has been produced effectively serializes your entire computation.

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

Sidebar

Related Questions

I am trying to solve readers-writers problem . The following functions is supposed to
I'm trying to solve a problem using java, iText, and the Java advanced imaging
I'm trying to solve the following problem in Redis. I have a list that
I am trying to solve the following problem with Puppet: I have multiple nodes.
I'm trying to solve the following problem: I have a download button that when
I'm trying to solve an instance of the weighted vertex cover problem using R
Trying to solve this problem: I have the following set of divs that when
I'm learning Sockets programming in Java. I'm trying to solve a problem that consists
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
Trying to solve this problem . I would like to learn how the bootstrapper

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.