• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

why start() ?? why not run() directly ??

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello folks.
previously i post this message in java Biggener forum i got a answer but i want answer in more technical depth.
//=============================================================
it's nessecery to call start() method for thread which is indirectly calling the run() method.we can call run() method directly but that time it is multithreading why ?
why is special about in calling start() method but not directly
run() method.
i am looking for very technical answer. please help me.
wating for warm response.
bye brany folks.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


I don't know as to what technical depth you are looking for. Let me try. If you call just run(), what you are doing is just like calling a method of an Object. run() is just a method. You provide the implementation for that method. Just by calling a run() method you are not creating a new thread.


To create a new thread you have to call the start() method of the Thread class. This start() method has the functionality to create a new thread, and the run method is called from a new thread. As simple as that.


If you are looking for anything more specific, please do ask.

Regards

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friend!
I think start is not really essential for a particular thread to run ,when u can directly create the instance of the thread in the init() and activate it there itself.I do in that manner only.
I am just trying dont mind much if ur not satisfied.
[This message has been edited by Rahul Mahindrakar (edited December 27, 2000).]
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Viswash
When you instantiate the Thread class any class extending the Thread class, the object created doesn't automatically become an active thread.
Like a process, a thread has stages-new,ready,running,waiting and terminate.
When you instantiate a thread, it is in the new stage and is not a full-fledged active thread.It is simply like any other object.If you are in the main thread and invoke the run method, the run method is executed in the main thread itself in the same channel of execution.
Now consider that the start method was invoked in a thread t (usually the main thread).When you invoke the start method, defined in the Thread class, this method has the functionality to create a new thread.It brings the thread into the ready state and puts the thread in the thread queue.A separate thread thus forks from the thread t in which the start method was invoked.
After this the CPU scheduler based on some algorithm will select one such thread from the queue and allocate the CPU to it. Only then the thread will go into running state and the run() method is executed.
When the run method ends, the thread terminates.Ofcourse the thread object doesn't die out-its other methods can still be accessed,only it's thread functionality ends(you can't invoke the start() again).
hope I sound clear enough or am I confusing you morE??
regards
Tanveer
 
vishwas bhatt
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's perfectly what i want Prabhakar thanks a lot.
hoping for the same response in the future too.

Bye.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more point is there..
If we call run() directly this will become the main thread and the code inside the run() method will be executed first as in a normal sequential program calling a method and the CPU time slicing will not take place and other threads will not be executed concurrently. The whole idea of multithreading will not take place.
Kalidas
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let us consider two cases here
Case 1:

Case 2 :

In Case 1 there is no concurrency
While in Case 2 Concurrency exists
Hope this makes it clear
[
Hi,
I have added the [ code] [ /code] UBB tags to your code. I hope you don't mind. It is recommended you use these tags when writing code. ]
[This message has been edited by Rahul Mahindrakar (edited January 15, 2001).]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI subram both u r programs works the sam ethey give the same output i dont find any difference in their output.
------------------
"Winners don't do different things
They do things differently"
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry to intervene, but the issue here is not the output but to explain multi-threading.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To put it in slightly different terms. Imagine you create 1000 instances of a class that extends thread. The run method sleeps for 5 seconds and then prints "hello". If you create a loop and run the start() method of the 1000 threads, the 1000 "hello"s will print out reasonably close to 5 seconds or so depending on the speed of the computer you are running on. If you run the run() method directly, then it will take at least 5,000 seconds to go through the 1,000 instances since they will not run as separate threads.
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats a good one Thomas
 
this is supposed to be a surprise, but it smells like a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic