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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:27:31+00:00 2026-06-11T00:27:31+00:00

I have recently inherited a large Java Application that has almost no Thread safety

  • 0

I have recently inherited a large Java Application that has almost no Thread safety in it. What I’m currently working on is getting all of the Threads to correctly handle being interrupted instead of using the very bad Thread.stop().

Part of the problem has been that I do not know every method call out there that clears the interrupt flag.

Currently I know that the following will clear the interrupt flag:

Thread.interrupted()
Thread.sleep(long)
Thread.join()
Thread.join(long)
Object.wait()
Object.wait(long)

What else am I missing? Thank you

  • 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-11T00:27:32+00:00Added an answer on June 11, 2026 at 12:27 am

    Part of the problem has been that I do not know every method call out there that clears the interrupt flag.

    It is important to clarify that the following methods clear the interrupt flag by just calling them:

    Thread.interrupted()
    Thread.isInterrupted(true) -- added to your list
    

    For this reason Thread.currentThread().isInterrupted() should always be used instead.

    The following methods will clear the interrupted flag by immediately throwing InterruptedException either if they were called and then the thread was interrupted or if the thread was already interrupted and then they were called (see junit code below). So it is not the method that clears the flag, throwing the exception does.

    Your initial list:

    Thread.interrupted()
    Thread.sleep(long)
    Thread.join()
    Thread.join(long)
    Object.wait()
    Object.wait(long)
    

    Added to your list:

    Thread.sleep(long, int)
    Thread.join(int, long)
    Thread.isInterrupted(true)
    Object.wait(int, long)
    BlockingQueue.put(...)
    BlockingQueue.offer(...)
    BlockingQueue.take(...)
    BlockingQueue.poll(...)
    Future.get(...)
    Process.waitFor()
    ExecutorService.invokeAll(...)
    ExecutorService.invokeAny(...)
    ExecutorService.awaitTermination(...)
    CompletionService.poll(...)
    CompletionService.take(...)
    CountDownLatch.await(...)
    CyclicBarrier.await(...)
    Semaphore.acquire(...)
    Semaphore.tryAcquire(...)
    Lock.lockInteruptibly()
    Lock.tryLock(...)
    

    Please note that the proper pattern with any code that catches InterruptedException is to immediately re-interrupt the thread. We do this in case others are relying on the thread.isInterrupted() method:

    try {
        ...
    } catch (InterruptedException e) {
        // immediately re-interrupt the thread
        Thread.currentThread().interrupt();
        // log the exception or [likely] quit the thread
    }
    

    JUnit code that demonstrates some of this:

    assertFalse(Thread.currentThread().isInterrupted());
    // you can do this from another thread by saying: someThread.interrupt();
    Thread.currentThread().interrupt();
    // this method does _not_ clear the interrupt flag
    assertTrue(Thread.currentThread().isInterrupted());
    // but this one _does_ and should probably not be used
    assertTrue(Thread.interrupted());
    assertFalse(Thread.currentThread().isInterrupted());
    Thread.currentThread().interrupt();
    assertTrue(Thread.currentThread().isInterrupted());
    try {
        // this throws immediately because the thread is _already_ interrupted
        Thread.sleep(1);
        fail("will never get here");
    } catch (InterruptedException e) {
        // and when the InterruptedException is throw, it clears the interrupt
        assertFalse(Thread.currentThread().isInterrupted());
        // we should re-interrupt the thread so other code can use interrupt status
        Thread.currentThread().interrupt();
    }
    assertTrue(Thread.currentThread().isInterrupted());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently inherited a large project using an sqlite3 database and currently I
I have recently inherited an ASP.Net app using Linq2SQL. Currently, it has its DataContext
I have inherited an app that generates a large array for every user that
I have recently inherited a J2EE Struts web app that was written back in
I recently inherited a large software project written in Java. The last developer used
I have recently inherited a MOSS 2007 based application primarily involving an InfoPath 2007
I have inherited a WiX project from a contractor that recently left. When I
I recently inherited a dinosaur Classic ASP site that has some fresh blood pumped
I have recently inherited a large web project. Before taking this on, I had
I have recently inherited some Java code and need to integrate it into a

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.