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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:25:11+00:00 2026-05-25T11:25:11+00:00

My unexperience with concurrency is quite clear, and I’m looking here for some help.

  • 0

My unexperience with concurrency is quite clear, and I’m looking here for some help.

I was writing a multithreaded application in Java while I was assailed by a doubt. Please look at this sample code (mixing pseudocode and Java):

Thead 1 body (portion):

/* It creates and starts thread Thread 2 */
if (!thread2.taskCompleted)
   thread2.wait();
/* continue execution... */

Thead 2 body:

class Thread2 extends Thread {

    volatile boolean taskCompleted = false;

    public void run() {
        /* It executes a complex task... */
        taskCompleted = true;
        notifyAll(); // notify waiting threads
    }

}

My concern is simple as that: what happens if the statements are executed in that order:

  1. Thread 1 starts Thread 2
  2. Thread 2 does some stuff, but doesn’t complete the task
  3. Thread 1 reads taskCompleted as false
  4. Thread 2 completes the task, raises the taskCompleted flag, and notifies (nobody)
  5. Thread 1 starts waiting. And never ends.

Please let me know if you have any ideas and/or it is a well-known scenario (exact duplicate?)

  • 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-25T11:25:12+00:00Added an answer on May 25, 2026 at 11:25 am

    The usage of the taskCompleted flag and wait()/nofiyAll() must be protected by a lock to avoid the scenario you describe.

    /* It creates and starts thread Thread 2 */
    synchronized (thread2) {
      if (!thread2.taskCompleted)
        thread2.wait();
    }
    /* continue execution... */
    

    and

    class Thread2 extends Thread {
    
      boolean taskCompleted = false;
    
      public void run() {
        /* It executes a complex task... */
        synchronized (this) {
          taskCompleted = true;
          notifyAll(); // notify waiting threads
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple downloader as Java applet. During some tests I discover that
I'm in the process of porting some old C code to Java and, due
I'm working on a java swing application which handles Customer management for a shop.
It may sound silly but I'm quite php outdated/unexperienced and coming from Java programming
I've previously written some selenium tests using ruby/rspec, and found it quite powerful. Now,
I think this is my overall .NET inexperience speaking here, but I cannot figure
NEWBIE ALERT! background: For the first time, I am writing a model that needs
I'm writing a sidebar for my site and I'm trying to check if: The
I have recently found myself in the position of explaining an (In-House) application I
After some searches via Google, I can't think of other keywords and am still

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.