Just a doubt pls solve if you have time

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Shuba

Just a doubt pls solve if you have time

Post 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(); 
         
        
   }
}

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Just a doubt pls solve if you have time

Post by admin »

Why do you expect Interrupted to be printed? You might want to take a look at InterruptedException API.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 70 guests