• Post Reply Bookmark Topic Watch Topic
  • New Topic

Concurrent behavior of threads

 
Gopal Shah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends
We know that threads run concurrently depending upon the scheduling mechanism.
So when does this concurrent execution start? After the creation of the Thread object or when the run() method of the thread object is being executed.
I mean, does the concurrency depends only on the start() method or does it depend on the run(). In case I create a subclass of Thread & don't override its run() method. Instead I create a method called myRun() and invoke this methd. Will it execute concurrently ?
For example I override the start() method and invoke the myRun() method in it.
a) public void start()
{
super.start();
myRun();
}
Will it produce equivalent result as the normal start method.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when does this concurrent execution start? After the creation of the Thread object or when the run() method of the thread object is being executed.
I mean, does the concurrency depends only on the start() method or does it depend on the run().

It depends on both. The start() method determines when the concurrent exectution begins - it starts immediately after start(). The run() method determines what the concurrent execution does - it does whatever the run() method says it does.
Creating a Thread object (without calling start()) does not cause any concurrent execution. Calling run() (again, without calling start()) also does not initiate any concurrent execution - the run() method just runs as part of the same thread that you called the run() method from.
In case I create a subclass of Thread & don't override its run() method. Instead I create a method called myRun() and invoke this methd. Will it execute concurrently ?
For example I override the start() method and invoke the myRun() method in it.
a) public void start()
{
super.start();
myRun();
}
Will it produce equivalent result as the normal start method.

No, definitely not. Well, because you do call super.start(), the normal start() is called, so a separate thread will be started to concurrently execute the run() method. But if that method doesn't do anything, then that thread is going to be pretty boring. Meanwhile in the main thread (the one that called start() in the first place) the myRun() method gets executed. But myRun() is not getting a separate concurrent thread to execute in. It's in the main thread. Which defeats the purpose of using a Thread at all - if you just wanted to execute things in the same thread, you could just call methods the old-fashioined way, with no threads involved.
 
Gopal Shah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U mean the concurrent behavior of a thread is present only while it is executing the run() method. When it exits out of run() method, the thread is no more active.
What if I make a call to run() method again, will the thread become active again ?
After the execution of run() method, at some later stage a call is made to myRun() method or any other method of the class, then will the thread be active or will the call belong to the main method (the one which invokes the myRun()), depicting non-concurrent behavior.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U mean the concurrent behavior of a thread is present only while it is executing the run() method.
Yes, and only if the run() was called indirectly as a result of calling start(). If you call run directly, there's no concurrency.
When it exits out of run() method, the thread is no more active.
What if I make a call to run() method again, will the thread become active again ?

No. You would have to call start() to gain any concurrency; start() will call run() for you. And start() can only be called once in the life of a thread - the second time, it will (or should) throw IllegalStateException.
After the execution of run() method, at some later stage a call is made to myRun() method or any other method of the class, then will the thread be active or will the call belong to the main method (the one which invokes the myRun()), depicting non-concurrent behavior.
It's non-concurrent. The only way, by which I mean the only way, for a Thread object to run code concurrently, is if you call the start() method. That's what it's there for. There are many, many other thing you could do with a Thread subclass, because Thread is an Object and you can create a subclass that does any of the other varied things that you can do with a class, but none of them have anything at all to do with concurrent behavior. If you want to get code to run concurrently, invoke the start() method - don't create other elaborate coding structures which avoid the start() method.
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of this confusion would be avoided if Sun developers gave run() protected access.
Gopal - A good rule of thumb is to pretend that the run() method is protected, so you cannot call it directly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!