Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread questions

 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thread questions:
Q.1 I don't understand interrupt() and internal flag and when should I use it?
Q.2 What does "No thread can make another thread yield" mean?
Q.3 Given:
public class MyRunnable implements Runnable
{
public void run()
{
//some code here
}
}
What is the proper way to create and start this thread?
Ans: new Thread(new MyRunnable()).start();
Why does it need to add new Thread as prefix?
Q.4 Given:
public class MyT extends Thread
{
public void run()
{
try
{
for(int i=1; i<5; i++)<br /> {<br /> System.out.print(i + " ");<br /> if(i>2)
interrupt();
sleep(1000);
if(interrupted())
break;
}
}
catch(InterruptedException e)
{
System.out.print("Caught");
}
}
}
Assume another object creates an instance of this Thread and starts it. Why does the outcome is:
1 2 3 caught
Thanks a lot.
Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic