Page 1 of 1

Just a doubt pls solve if you have time

Posted: Mon Jun 27, 2011 3:40 pm
by Shuba
I found this question in Exam 4

Code: Select all

class Hello implements Runnable{
 int i;
 public void run(){
  try
  {
    Thread.sleep(3000);
  }
  catch (InterruptedException e){}
  i = 20;
 }
}

public class T5
{
 static public void main(String[] args) throws Exception{
  Hello h = new Hello();
  Thread t = new Thread(h);
  t.start();
  t.join();
  System.out.println("h.i = " + h.i);
 }
}
I tried to create and run code similar to the above but I expect "Interrupted" to be printed but that is not happening.

Code: Select all

class ThreadTest implements Runnable{

    public void run(){
        try{ 
           Thread.sleep(1000);
        }
        catch(InterruptedException e){
            System.out.println("Interrupted ");
        }
    }

    public static void main(String [] args) throws Exception{
         ThreadTest h = new ThreadTest();
         Thread t = new Thread(h);
         t.setName("Billy");
         t.start();
         t.join(); 
         
        
   }
}

Re: Just a doubt pls solve if you have time

Posted: Mon Jun 27, 2011 9:05 pm
by admin
Why do you expect Interrupted to be printed? You might want to take a look at InterruptedException API.