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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:23:47+00:00 2026-05-29T06:23:47+00:00

I have a scenario where I have one thread that loops between waiting and

  • 0

I have a scenario where I have one thread that loops between waiting and executing a task. However, I would like to interrupt the wait for the thread (skip the rest of the wait if you will) and continue on to doing the task.

Anyone have any ideas how this could be done?

  • 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-29T06:23:48+00:00Added an answer on May 29, 2026 at 6:23 am

    This is what Thread.interrupt is for:

    import java.util.Date;
    
    
    public class Test {
    
        public static void main(String [] args) {
            Thread t1 = new Thread(){
                public void run(){
                    System.out.println(new Date());
                    try {
                        Thread.sleep(10000); // sleep for 10 seconds.
                    } catch (InterruptedException e) {
                        System.out.println("Sleep interrupted");
                    }
                    System.out.println(new Date());
                }
            };
    
            t1.start();
            try {
                Thread.sleep(2000); // sleep for 2 seconds.
            } catch (InterruptedException e) {
                e.printStackTrace();  
            }
            t1.interrupt();
        }
    }
    

    Thread t1 will only sleep for 2 seconds, since the main thread interrupts it. Keep in mind that this will interrupt many blocking operations such as IO.

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

Sidebar

Related Questions

There is scenario, I have two threads both are using same mutex. One thread
I have one thread that add() s objects into a LinkedList queue, and another
My scenario: I have one color background image JPG. I have one black text
I have a similar scenario as this one: public class TestLinq2Xml { private XElement
I have a scenario where I need to upload a file from one web
We have a scenario where multiple devices are connected to one com port. Each
I have a scenario where a user will have access to a one-time-url. When
Say I have a simple client/server scenario with one method: // client code $client
Scenario I have two wrappers around Microsoft Office, one for 2003 and one for
I have the following scenario: Entities are loaded from the database. One of them

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.