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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:58:14+00:00 2026-05-24T18:58:14+00:00

I started learning java and I am now at the concurrency chapter. After reading

  • 0

I started learning java and I am now at the concurrency chapter. After reading some stuff about concurrency I tried an example of my own.

public class Task implements Runnable{

public void run() {
    while(!Thread.interrupted()) {
        try {
            System.out.println("task");
            TimeUnit.SECONDS.sleep(2);
        }catch (InterruptedException e) {
            System.out.println("interrupted");
        }
    }
}

}

public static void main(String[] args) throws Exception {
    ExecutorService exec = Executors.newCachedThreadPool();
    exec.execute(new Task());
    TimeUnit.SECONDS.sleep(10);
    exec.shutdownNow();
}

The problem is that i was expecting to see the following output:

task
task
task
task
task
interrupted

but after I get this, the program continues printing until I close it.
So, my question is what am I doing wrong? why does the program continues printing?

  • 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-24T18:58:16+00:00Added an answer on May 24, 2026 at 6:58 pm

    The section about interrupts in the Java tutorials about concurrency explain the problem quite well:

    The Interrupt Status Flag

    The interrupt mechanism is implemented using
    an internal flag known as the interrupt status. Invoking
    Thread.interrupt sets this flag. When a thread checks for an interrupt
    by invoking the static method Thread.interrupted, interrupt status is
    cleared. The non-static isInterrupted method, which is used by one
    thread to query the interrupt status of another, does not change the
    interrupt status flag.

    By convention, any method that exits by throwing an
    InterruptedException clears interrupt status when it does so.
    However,
    it’s always possible that interrupt status will immediately be set
    again, by another thread invoking interrupt.

    So when you catch the InterruptedException within the loop, the interrupted status is already reset and thus, the next call of Thread.interrupted() will return false, which in turn keeps the while loop running. To stop the loop, you have the following options:

    • Use break to exit the loop
    • Use return to exit the whole method
    • Move the try-catch-block to outside the while loop (as suggested by Nathan Hughes)
    • Call interrupt() on the current thread to set the interrupted flag again
    • Use a separate boolean to control the loop and set that flag accordingly in the catch-block
    • Make the task a recurring task by using a ScheduledExecutorService and dropping the loop from the run-method of your Runnable
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently, I started learning some Java. From what I've already learned about JVM it
I started learning c++ about 3 weeks ago after 2 years of java. It
I am learning java and started with classes and I am now having some
I just started learning C++ (coming from Java ) and am having some serious
I just started learning Java and I'm confused about the topic of platform independence.
I recently started learning Java and found it very strange that every Java public
I have just started learning java and i have about 8 months time. My
I just recently started learning Java. I have a question which is more about
I've only started learning Java about 3 months ago and this is my first
I have just started learning about Java server faces and successfully tested out my

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.