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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:01:39+00:00 2026-06-11T01:01:39+00:00

As you can tell I’m new to multithreading and a bit stuck here. For

  • 0

As you can tell I’m new to multithreading and a bit stuck here. For my program I need a thread (PchangeThread in the below example) that can be toggled on and off from another thread at any point during execution of the program.
The thread should be suspended on start and resume when pixelDetectorOn() is called.

The two threads will most likely not need to share any data except for a start/stop flag. I included a reference to the main thread anyway, just in case.

However, in the below code the only message that is ever output is “before entering loop”, which indicates that the thread never wakes up from wait() for some reason. I’m guessing this is some kind of locking problem but I haven’t been able to figure out what exactly is going wrong. Locking on this.detector from the main thread gives me the same result. Also I’m wondering if the wait()/notify() paradigm is really the way to go for suspending and waking the thread.

public class PchangeThread extends Thread {
  Automation _automation;
  private volatile boolean threadInterrupted;

  PchangeThread(Automation automation)
  {
    this._automation = automation;
    this.threadInterrupted = true;
  }

  @Override
  public void run()
  {
    while (true) {
      synchronized (this) {
        System.out.println("before entering loop");
        while (threadInterrupted == true) {
          try {
            wait();
            System.out.println("after wait");
          } catch (InterruptedException ex) {
            System.out.println("thread2: caught interrupt!");
          }
        }
      }
      process();
    }
  }

  private void process()
  {
    System.out.println("thread is running!");

  }

  public boolean isThreadInterrupted()
  {
    return threadInterrupted;
  }

  public synchronized void resumeThread()
  {
    this.threadInterrupted = false;
    notify();
  }
}

resumeThread() is called from the main thread the following way:

public synchronized void pixelDetectorOn(Context stateInformation) {        
        this.detector.resumeThread();
}

detector is a reference to an instance of PchangeThread.
The “detector”-thread is instantiated in the program’s main module the following way:

detector=new PchangeThread(this);
  • 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-11T01:01:41+00:00Added an answer on June 11, 2026 at 1:01 am

    As you said, you need to protect access to the shared flag. You declared threadInterrupted volatile, but than are still using syncronized. You only need one. I prefer to just use syncronized as it makes things simpler. Multi-threading is complicated enough, keep it simple unless you know you need more complicated controls. This means that any time threadInterrupted is read or written to, the access should be synchronized. Currently, you are not doing that in setThreadInterrupt() and isThreadInterrupted().

    Secondly, you want to synchronize on as small of a code block as possible. Inside of run(), you are synchronizing over the inner loop. In actuality, you only need to to synchronize on the read of threadInterrupted. When the implementation of isThreadInterrupted() is fixed as mentioned above, you can use that directly and remove the synchronized block from the inner loop.

    The fact that you are synchronizing on the inner loop, is the error that is causing your code to never print “thread is running!”. PchangeThread acquires the lock on itself and calls wait() to suspend the thread. However, the thread is still holding the lock at this point. At some point later, the main thread calls resumeThread() in order to restart the thread. However, that method can not begin its execution because it must first wait to acquire the lock. However, it will never get the lock until the PchangeThread is notified.

    You are providing two ways to set threadInterrupted, but only one of them notifies the thread when the value is set to false. Do you really need setThreadInterrupt()? I expect you don’t. If you keep it, it should act the same as resumeThread() when the argument is false.

    Lastly, it is better to lock on a private object instead of the instance itself. You have complete control over the private lock object. However, anyone with a reference to your thread instance could also use it as the lock for a synchronized block, which could potentially lead to a hard to find deadlock.

    Your code altered to use my edits:

    public class PchangeThread extends Thread {
      private final Object _lock = new Object();
      Automation _automation;
      private final boolean _threadInterrupted;
    
      PchangeThread(Automation automation)
      {
        _automation = automation;
        _threadInterrupted = true;
      }
    
      @Override
      public void run()
      {
        while (true) {
          System.out.println("before entering loop");
          while (isThreadInterrupted()) {
            try {
              wait();
              System.out.println("after wait");
            } catch (InterruptedException ex) {
              System.out.println("thread2: caught interrupt!");
            }
          }
          process();
        }
      }
    
      private void process()
      {
        System.out.println("thread is running!");
    
      }
    
      public boolean isThreadInterrupted()
      {
        synchronized (_lock) {
          return _threadInterrupted;
        }
      }
    
      public void resumeThread()
      {
        synchronized (_lock) {
          _threadInterrupted = false;
          notify();
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody tell me how to make my compiler find the libs that it
Can Somebody can tell what is need of application class . I have read
Somebody can tell me an example of using locking mechanism based on futex? (for
Can anyone tell me, or send me to a page, that can tell me
How can tell if my object's value is a float or int? For example,
Google says that you can tell it to not provide a cached link in
please any one can tell me video format that play on any browser ,
I need someone who can tell me what I'm missing. I have this scalar
From what I can tell, the difference between PyList_SetItem and PyList_SETITEM is that PyList_SetItem
Anyone can tell me which one is better (JAXB or Apache XMLBeans) taking in

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.