• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

threads

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

for the above program with respective to program no.4 the output generated
is "1" but wht my question is we know that when a thread encounters "run" method in the program a separate thread will be generated which executes the code in the "run" method but in the above program if that is the case a separate thread should be generated and should execute the code in the "run" method and before it happens "System.out.println(a.i)" should print the output as "0"




Hi,
for the above programs how the output is generated,anybody please provide some explanation for the above
[ April 29, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
4)


when a thread encounters "run" method in the program a separate thread will be generated which executes the code in the "run" method but in the above program if that is the case a separate thread should be generated


No, when a thread's run method is called, a separate thread of execution is not started. A separate thread of execution is started only when u call start() method on it. So, in ur case the behavior is as of a normal program.

1) Just based on my reasoning above, a separate thread will be started only if u call start on it. So u can see that u calling run method on A directly, no new thread is running, its the B thread which runs and so u get the first two outputs having thread name as that of B. For the last line, a new thread is being created and so u get the given output.

2) In this case i guess u r getting a compiler error. If u have read overriding u would have got the explanation. U r trying to override the method run() and throwing broader checked exceptions than run() method in Thread class. Infact, the run() method in Class Thread doesnt throw any exception, so when u override it u shouldnt throw any checked exception.


Hope its clear
 
Venkat Ramsimha
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Animesh really its wonderful explanation
 
Venkat Ramsimha
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Animesh very clear explanation
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic