Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: relation between interrupt status and sleep method

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default relation between interrupt status and sleep method

    Hi !
    --->My doubt Is on relation ship between sleep method and interrupt status flag.
    --->I am asking this doubt based on below two programmes.
    --->In programme1, I have called sleep method with out clearing interrupt status .Then InterruptedException was thrown.
    --->In programme2 ,I have called sleep method after clearing interrupt status flag using interrupted method.Then InterruptedException was not thrown.
    Is there any relation ship between sleep method and interrupt status flag?
    Does sleep method throws Interrupted exception based on interrupt status flag?
    ---------------------------------------------------------------
    Programme1:
    class even extends Thread
    {
    public void run()
    {
    for(int i=10;i<=20;i=i+2)
    {
    System.out.println(i);
    this.interrupt();
    try
    {
    Thread.sleep(1000);
    }
    catch(InterruptedException ie)
    {
    System.out.println("interrupted");
    }
     
    }
    }
    }
    class interrupt1
    {
    public static void main(String args[])
    {
    even e1=new even();
    e1.start();
    }
    }
    Output:
    10
    interrupted
    12
    interrupted
    14
    interrupted
    16
    interrupted
    18
    interrupted
    20
    Interrupted
    ---------------------------------------------------------------
    Programme2:
    class even extends Thread
    {
    public void run()
    {
    for(int i=10;i<=20;i=i+2)
    {
    System.out.println(i);
    this.interrupt();
    Thread.interrupted();
    try
    {
    Thread.sleep(1000);
    }
    catch(InterruptedException ie)
    {
    System.out.println("interrupted");
    }
     
    }
    }
    }
    class interrupt2
    {
    public static void main(String args[])
    {
    even e1=new even();
    e1.start();
    }
    }
    output:
    10
    12
    14
    16
    18
    20
    ---------------------------------------------------------------


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: relation between interrupt status and sleep method

    Please edit you post and properly align the code. nested statements should be indented 3-4 spaces.
    All statements should NOT start in the first column.


    This topic is discussed in the API doc. Do you have any questions about the text of the API doc? If so, copy the text, paste it here and ask your questions.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default relation between interrupt status and sleep method

    Hi!
    I am a beginner of java.
    Now I am studying multi threading.
    I want to know relationship between sleep and interrupt status flag".
    I have referred so many books before asking this question here.
    But I could not find the relation between sleep and interrupt status flag.
    I need a book which explains such type of depth topics.
    can u suggest such type of java book?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: relation between interrupt status and sleep method

    Why did you start a new thread before responding on the other thread?

    Threads merged.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Sleep learn Java?
    By hdog100 in forum The Cafe
    Replies: 3
    Last Post: August 6th, 2012, 11:31 PM
  2. Alternative to thread.sleep?
    By jm24 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2012, 01:42 AM
  3. [SOLVED] Making an interrupt back to a class
    By simonthecat in forum Object Oriented Programming
    Replies: 3
    Last Post: June 1st, 2011, 07:53 PM
  4. Problem with thread.sleep()
    By stormforce in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 19th, 2011, 02:10 AM