This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic