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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:26:16+00:00 2026-05-26T03:26:16+00:00

I was reading some java thread interruption and I don’t understand some stuff. Hope

  • 0

I was reading some java thread interruption and I don’t understand some stuff. Hope someone will explain me. So, it’s done the following code

public Integer getInteger(BlockingQueue<Integer> queue) {
    boolean interrupted = false;
    try {
        while (true) {
            try {
                return queue.take();
            } catch (InterruptedException e) {
                interrupted = true;
                // fall through and retry
            }
        }
    } finally {
        if (interrupted)
            Thread.currentThread().interrupt();
    }
}

The explanation is as follows :

Activities that do not support cancellation but still call
interruptible blocking methods will have to call them in a loop,
retrying when interruption is detected. In this case, they should save
the interruption status locally and restore it just before returning,
as shown in listing. rather than immediately upon catching
InterruptedException. Setting the interrupted status too early could
result in an infinite loop, because most interruptible blocking
methods check the interrupted status on entry and throw
InterruptedException immediately if it is set. (Interruptible methods
usually poll for interruption before blocking or doing any significant
work, so as to be as responsive to interruption as possible.)

I don’t understand why we should save the interrupted status locally.
I would be glad to hear some explanation.

  • 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-26T03:26:17+00:00Added an answer on May 26, 2026 at 3:26 am

    By the design the method could not ever throw the InterruptedException. So it means we always expect take a value from the queue. But somebody could want the thread to be interrupted, that’s why we have to save-restore the interrupted state after we have finally took a value from the queue.

    So, the thread get finished only after taking a value from the queue.

    UPDATE: Look into the take() method implementation. It has the following as the first statements:

    public final void acquireInterruptibly(int arg) throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
    ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Despite me reading wikipedia and such, I still don't really understand what Thread Safety
I was reading some Java recently and came across something (an idiom?) new to
I remember reading in some Java book about any operator other than 'instanceof' for
I am reading image files in Java using java.awt.Image img = Toolkit.getDefaultToolkit().createImage(filePath); On some
After reading some threads on misuses of exceptions (basically saying you don't want to
I'm reading some code in the Ogre3D implementation and I can't understand what a
I was reading some of the concurrency patterns in Brian Goetze's Java Concurrency in
So I was reading some other thread about using dot notation to access an
I am reading some servlet text regarding scope of attributes in Java servlet. In
I'm trying to write some Thread Management utility, and reading up on ThreadMXBean. http://download.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html#dumpAllThreads(boolean,

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.