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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:58:10+00:00 2026-06-02T14:58:10+00:00

I have a thread that does several things. One of them is to just

  • 0

I have a thread that does several things. One of them is to just sleep for some time.
After a normal sleep it calls delayFinished() method, but if the sleep is interrupted then delayFinished() should not be called. I also need a method that aborts the sleep, that may be called by other threads.

So this is the implementation that captures my intent, but I don’t think it would work:

public class MyThread extends Thread {
   private boolean sleeping=false;
   private Object sleepingControl=new Object();

   //... other unrelated stuff...

   private void delay() {
      try {
          synchronized(sleepingControl) {
             sleeping=true;         
             sleep(delay);
             sleeping=false;                        
             delayFinished();
          }
      } catch (InterruptedException e) {
          sleeping=false;
      }
    }
    public void abortDelay() {
          synchronized(sleepingControl) {
             if (sleeping)          
                interrupt();
          }
    }
 }

If delay() is called, and while it’s sleeping, abortDelay() is called by another thread (the main use case), abortDelay() would hang on the synchronized statement since the caller of delay() owns that monitor and has not relinquished it yet.

On the other hand, if delay is implemented this way:

   private void delay() {
      synchronized(sleepingControl) {
         sleeping=true;         
      }
      try {              
             sleep(delay);

it’s possible that delay() is called, finishes the synchronized block setting
sleeping to true, but then abortDelay() is called, and it would call interrupt()
even though the thread has not yet started its sleep.

Can anyone suggest any improvements to these attempts?

  • 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-02T14:58:13+00:00Added an answer on June 2, 2026 at 2:58 pm

    You need to investigate Object.wait()/notify() instead of using sleep(). With wait() one of the crucial things is that it releases the lock on the object while waiting allowing another thread to acquire the lock and wake it up with notify().

    e.g.

    public class MyThread extends Thread {
       private boolean aborted = false;
       private final Object sleepingControl=new Object();
    
       //... other unrelated stuff...
    
       private void delay() {
          try {
              synchronized(sleepingControl) {
                 sleepingControl.wait(delay);
    
                 if (!aborted)
                     delayFinished();
              }
          } catch (InterruptedException e) {
          }
        }
        public void abortDelay() {
              synchronized(sleepingControl) {
                 aborted = true;
                 sleepingControl.notify();
              }
        }
     }
    

    This is also not the whole story as wait() has a curious implementation quirk where it can spuriously wake up. So you need to manually loop the wait() call and if it returns check to see if the time has actually expired.

    In reality I think you can achieve the above simpler with the java.util.concurrent classes. Take a look at the Lock class.

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

Sidebar

Related Questions

I have a thread that does the following: 1) Do some work 2) Wait
I have a windows form on the main thread and another thread that does
I have one thread that is receiving data over a socket like this: while
I have a thread that downloads some images from internet using different proxies. Sometimes
I have a background thread that handles communication with an external service. Each time
I have a worker thread that may be active for short bursts of time
I'm running a java program on many computers that interact between them. After several
well i mean i have several sections of code that use some variable i
I have a multi-threaded program, where I have one thread to watch over several
I have an iOS class which does some calculations in a separate thread while

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.