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

I'm going through MSIL and noticing there are a lot of nop instructions in
Going through some WCF hosting questions on StackOverflow, I've seen Dublin mentioned a few
While going through university and from following the development of SO, I've heard a
After going through the Appendix A, C# Coding Style Conventions of the great book
I'm going through the problems on projecteuler.net to learn how to program in Erlang,
We're going through a massive migration project at the minute and trying to validate
I was going through the AWDR book on web development with ruby on rails
I was going through some code and came across a scenario where my combobox
I'm going through SICP and I'd like to have an interpreter analogous to the
I'm going through some old stored procedures at work and constantly come across CASE

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.