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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:32:07+00:00 2026-05-16T16:32:07+00:00

I am going through the kathy sierra SCJP 1.5 Chapter 9(threads) and there it

  • 0

I am going through the kathy sierra SCJP 1.5 Chapter 9(threads) and there it is mentioned as:

Notice that the sleep() method can throw a checked InterruptedException
(you’ll usually know if that is a possibility, since another thread has to explicitly do
the interrupting), so you must acknowledge the exception with a handle or declare

I just need a sample program to know when it happens (which i can run on my machine)?

I googled but could not find any sample code to test this functionality..

Thanks in Advance

  • 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-16T16:32:07+00:00Added an answer on May 16, 2026 at 4:32 pm

    Here’s an example:

    public class Test
    {
        public static void main (String[] args)
        {
            final Thread mainThread = Thread.currentThread();
    
            Thread interruptingThread = new Thread(new Runnable() {
                @Override public void run() {
                    // Let the main thread start to sleep
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    mainThread.interrupt();
                }
            });
    
            interruptingThread.start();
    
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                System.out.println("I was interrupted!");
            }
        }
    }
    

    To walk through it:

    • Set up a new thread which will sleep for a short time, then interrupt the main thread
    • Start that new thread
    • Sleep for a long-ish time (in the main thread)
    • Print out a diagnostic method when we’re interrupted (again, in the main thread)

    The sleep in the main thread isn’t strictly necessary, but it means that the main thread does get to really start sleeping before it’s interrupted.

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

Sidebar

Related Questions

After going through some examples on the web I realize that there is a
When going through some sample code, notice that setting the main Window Title in
While going through MSDN I found that in button properties useSubmitBeheviour there is written
While going through Wikipedia's list of sorting algorithms I noticed that there's no stable
Going through this tutorial https://developers.google.com/web-toolkit/doc/latest/tutorial/RPC it is mentioned that to set up a call
Going through happstack-lite tutorial : we build functions that have return type of ServerPart
While going through SWig generated wrappers, I find that the PInvokes don't have any
Going through some WCF hosting questions on StackOverflow, I've seen Dublin mentioned a few
While going through the exercises, I came across something that, even after research, I
While going through one project, I have seen that the memory data is 8

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.