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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:09:44+00:00 2026-06-09T15:09:44+00:00

I intended to write a shared buffer between a producer and a consumer thread.

  • 0

I intended to write a shared buffer between a producer and a consumer thread. Here are my codes:

class PQueue
{
    Token token;
    boolean flag = false;   // false: add, true: poll

    PQueue()
    {
        token = null;
    }

    synchronized void add(Token token)
    {
        if(flag == true)
        {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        flag = true;
        notify();
        this.token = token;
    }

    synchronized Token poll()
    {
        if(flag == false)
        {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        flag = false;
        notify();
        return this.token;
    }
}

I am new to multithreading. Is there any potential concurrency bugs? Is this a “standard/common” way to achieve this goal? Or is there any much simpler and more efficient way?

  • 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-06-09T15:09:46+00:00Added an answer on June 9, 2026 at 3:09 pm

    Take a look at the java.util.concurrent package, in particular the BlockingQueue interface and the classes that implement it. These are meant for passing messages from one thread to another. The SynchronousQueue is exactly what you’re trying to implement.

    Your own implementation has a few flaws. For one, both the shared variables should be declared volatile to ensure that changes on one thread are seen by the other. And your if (flag == false) and if (flag == true) tests should actually be while loops, because wait() can wake up spuriously when notify() hasn’t actually been called.

    Rather than having a separate flag variable, I’d suggest just setting the token to null to indicate that there’s no object. And rather than catching, printing, and blindly continuing in the face of InterruptedException, I’d recommend letting both methods just throw that exception if it happens. These are blocking methods, and it’s the caller’s responsibility to handle the possibility of a blocking method being interrupted.

    Also, I don’t know what your Token class is, but nothing in your queue actually depends on its type. It’d make more sense to define a generic PQueue<T>, and then use PQueue<Token> if you want to pass tokens.

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

Sidebar

Related Questions

I intended to create a class which only have static members and static functions.
I'm trying to write a custom Panel class for WPF, by overriding MeasureOverride and
Is it possible to write an asInstanceOfOption method that would do what is intended
I want to write something like the following: internal class InternalData { } public
I'm trying to write a python class which uses a decorator function that needs
I am trying to write a quadtree sparse matrix class. In short, a quadtree_matrix<T>
Background: I've inherited a web application that is intended to create on-the-fly connections between
I'm trying to write a conversion operator function template in a class and running
I wrote a simple script that is intended to do hierarchical clustering on a
I intended for this function to call my MVC action method that returns a

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.