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.
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.
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.
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)
Joined: Nov 30, 2000
It's perfectly what i want Prabhakar thanks a lot. hoping for the same response in the future too.
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
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).]
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.