• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

My confusion about Thread

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm moving this fancy stuff over to the Threads and Synchronization forum...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic