• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Thread problem

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can somebody explain the output for the code below. I dont think if we can call the run method explicity. it is implicitly called when start() method is invoked.
Thanks in advance.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi neha

First of all,we can explicitly call run().Its just like some other x() in a class.
But note that calling the run() in the code does not start a new Thread.

new A().run();
This calls the run() method and doesnot start a new thread.

A new thread starts running only by calling the start() method.

Thanks
Praveen SP
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI.........................

First of all make it clear that we can call a run() method explicitly.
But sun microsystem programmers have written a lots of code in start() method in native languauges for allocation of memory and for certain other important criteria.So they suggest using start() method for invoking a thread.If we are using run() method for invoking thread there might occur some problem with your code in the operating syatem environment.
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neha, it is very simple.

When you call run() directly it does not create a new thread. Calling start() only creates a new thread.

So in the code below you start a new thread with name T1. Then:
new A().run();---just calls run(), no new thread
new Thread(new A(),"T2").run();---just calls run(), no new thread
new Thread(new A(),"T3").start();-----creates a new thread with name T3



Hope i'm able to provide some meaningfool explanation.

regards,
Gitesh
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic