Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic