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

  • Home
  • SEARCH
  • 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 7852047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:19:37+00:00 2026-06-02T19:19:37+00:00

I am trying to construct two threads, thread A is the main thread and

  • 0

I am trying to construct two threads, thread A is the main thread and thread B is the second thread, thread B is updating a variable through a time consuming function (this variable should be shared between both threads, because eventually thread A needs to use that variable as well), but I want thread A to terminate thread B if thread B takes too long to complete (using an exception).

What I tried is the following:

Thread thread = new Thread() {
     public void run() {
         /// run something that could take a long time
     }
};

synchronized (thread) {
    thread.start();
}

System.err.println("Waiting for thread and terminating it if it did not stop.");
try {
   thread.wait(10000);
} catch (InterruptedException e) {
   System.err.println("interrupted.");
}

Should that give the expected behavior of terminating a behavior in case it has run more than 10 seconds? The thread object gets deleted after the wait, because the method that runs the thread returns.

Right now, what happens with this code is that I always get java.lang.IllegalMonitorStateException on the wait(10000) command.

  • 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-02T19:19:38+00:00Added an answer on June 2, 2026 at 7:19 pm

    You will always get a IllegalMonitorStateException if you are calling wait() on an object that you are not synchronized on.

    try {
        // you need this to do the wait
        synchronized (thread) {
           thread.wait(10000);
        }
    } catch (InterruptedException e) {
        System.err.println("interrupted.");
    }
    

    If you are waiting for the thread to finish then you probably are trying to do a:

    try {
        thread.join(10000);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        System.err.println("interrupted.");
    }
    

    Unfortunately, you do not know at that point if the thread is running because join doesn’t return whether or not it timed out (grumble). So you need to test if the thread.isAlive() after the join.

    If you are asking how you can cancel the thread if it runs for longer than 10000 millis, then the right thing to do is use thread.interrupt(). This will cause any sleep() or wait() methods to throw an InterruptedException and it will set the interrupt flag on the thread.

    To use the interrupt flag your thread should be doing something like:

       while (!Thread.currentThread.isInterrupted()) {
           // do it's thread stuff
       }
    

    Also, it is always a good pattern to do something like the following because once the InterruptedException is thrown, the interrupt flag has been cleared:

    } catch (InterruptedException e) {
        // set the interrupt flag again because InterruptedException clears it
        Thread.currentThread.interrupt();
        System.err.println("interrupted.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to construct a SpannableString such that it looks like this: Two
I have two Line objects in C# WPF, and I'm trying to construct a
I'm trying to construct the flexible URL routing. So the url like en-US/ctrl/act/1/2 should
I'm trying to write a function that, given two quadtrees representing images, will output
I'm trying to construct two components that communicate via UDP sockets. Component 1 is
I've spent the last two days trying to find a solution to this problem
I'm trying to construct some app for time schedules of public transport. Most it
I am trying to run a template function on a separate thread but IntelliSense
I'm trying to construct a query that will map two columns, one, a date
Suppose two integers x and N. I'm trying to determine how to construct an

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.