Two Laptop Bag*
The moose likes Threads and Synchronization and the fly likes My confusion about Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "My confusion about Thread" Watch "My confusion about Thread" New topic
Author

My confusion about Thread

Roger Zhao
Ranch Hand

Joined: Aug 05, 2003
Posts: 73
Hi, all:
Something I am not sure about the question:
If you have an applicaton which execute the following codes:
Thread myT=new Thread();
myT.start();
Select all of the following statements that are correct:
[a]. The Thread myT is now in a runnable state.
[b]. The Thread myT has the NORM_PRIORITY priority.
[c]. The Thread will die without accemplishing anything
[d]. The run method in the class where the statement occurs will be executed.
---Anwser is [a],[c]
Try the following codes:
class test {
public static void main(String argv[]){
Thread t=new Thread();
t.start();
System.out.println(t.getPriority());
System.out.println(t.isAlive() );
}
}
Output:
5
true
About option [a]. right with no doubts.
Q1: About option [b]. Default priority is 5 which will set to the new threads,right?
Q2: About option [c], The run method in the Thread class will be executed. The run method in the Thread class is empty so myT will die as soon as it runs. But the above codes show it is still alive after it runs!! why?
Q3: Also wanna know the explanation about optin [d]?

help! thanks!


"There is a will,there is a way!"<br />SCJP1.4
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
You are making assumptions in your example that don't exist in the question. The question doesn't give you any other code, so, you have to assume it could be anything.
Q1 - If this Thread is started in another Thread that had it's priority set to something other than NORM_PRIORITY, it would be started with that priority since the code doesn't set it before calling start.
Q2 - There is no garentee the Thread scheduler ever ran this Thread before the call to isAlive. You are correct in assuming that if the Thread did indead run, isAlive would return false there. You can increase the chance that the created Thread will run before this method call by putting a Thread.sleep(50); just before it.
Q3 - This code could be inside a class that implements Runnable. In that case, the class would have a run method but, the Thread isn't acting on a Runnable class so, the run method in the created Thread object is run as you speculate.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I'm moving this fancy stuff over to the Threads and Synchronization forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My confusion about Thread