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.
Mahanthi Bukkapatnam
Greenhorn
Joined: Dec 19, 2000
Posts: 11
posted
0
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
Surya Prabhakar
Greenhorn
Joined: Dec 21, 2000
Posts: 7
posted
0
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).]
I am a novice user.I hope people out there will help me.
Tanveer Rameez
Ranch Hand
Joined: Dec 11, 2000
Posts: 158
posted
0
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
Author of JPhotoBrush Pro (www.jphotobrushpro.com)
vishwas bhatt
Ranch Hand
Joined: Nov 30, 2000
Posts: 129
posted
0
It's perfectly what i want Prabhakar thanks a lot. hoping for the same response in the future too.
Bye.
Kalidas Pavi
Ranch Hand
Joined: Nov 20, 2000
Posts: 42
posted
0
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
Morgan Subram
Greenhorn
Joined: Jan 03, 2001
Posts: 12
posted
0
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).]
sunilkumar ssuparasmul
Ranch Hand
Joined: Dec 13, 2000
Posts: 142
posted
0
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"
"Winners don't do different things<br /> They do things differently"
Anand Krishna
Greenhorn
Joined: Jan 14, 2001
Posts: 5
posted
0
I am sorry to intervene, but the issue here is not the output but to explain multi-threading.
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
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.