| Author |
Callin start from run method In case of threads Problem
|
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
i am not able to get the code
start in run is called on which thread MAIN or THREAD-0
Getting the following output at all times,i ran it in IDE and then from command-prompt 10 times..
can there be different output for this code ???
output:
mainthreadNO
Thread-0threadNO
mainwaitingjava.lang.IllegalThreadStateException
Thread-0waitingjava.lang.IllegalThreadStateException
|
OCPJP 6.0 93%
OCPJWCD 5.0 98%
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
What do you expect? You can't start a thread more than once!
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
what i meant Abimaran was that
Is it possible that thread abc runs first and then main thread
can this be the sequence
1.first,abc.start() runs,so control goes to run method
2.Thread-0threadNO is printed
3.then,it sees start so,IllegalThreadStateException occurs
4.we get Thread-0waitingjava.lang.IllegalThreadStateException
5.then JVM,selects the main thread and executes abc.run();
6.main thread calls run method,
7.prints mainthreadNO
8.and it sees the start and then
mainwaitingjava.lang.IllegalThreadStateException prints,since main has already started
output:
Thread-0threadNO
Thread-0waitingjava.lang.IllegalThreadStateException
mainthreadNO
mainwaitingjava.lang.IllegalThreadStateException
------------------------------------------------------------------------
Is the following output
--------------
-
-
v
mainthreadNO
Thread-0threadNO
mainwaitingjava.lang.IllegalThreadStateException
Thread-0waitingjava.lang.IllegalThreadStateException
means this
1.Thread abc runs its run method
2.Thread-0threadNO is printed
3.since run is not synchronized,Jvm can select the main thread and move thread-0 to runnable and main running state
4.abc.run() is executed and
5.mainthreadNO is printed
6. start causes
mainwaitingjava.lang.IllegalThreadStateException
7.then control is given back to thread-0,it too sees the start
8.Thread-0waiting[b]java.lang.IllegalThreadStateException [/b]is printed
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
I think, you are struggling with the threads outputs. You can't predict the behavior of executions! And check you previous post here!
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
Abimaran ,i didn't get answer to my questions
Query-1.
Is it possible that thread abc runs first and then main thread
Query-2.
can this be the sequence
1.first,abc.start() runs,so control goes to run method
2.Thread-0threadNO is printed
3.then,it sees start so,IllegalThreadStateException occurs
4.we get Thread-0waitingjava.lang.IllegalThreadStateException
5.then JVM,selects the main thread and executes abc.run();
6.main thread calls run method,
7.prints mainthreadNO
8.and it sees the start and then
mainwaitingjava.lang.IllegalThreadStateException prints,since main has already started
output:
Thread-0threadNO
Thread-0waitingjava.lang.IllegalThreadStateException
mainthreadNO
mainwaitingjava.lang.IllegalThreadStateException
-----------------------------------------------------------------------------
Query-3.
is my interpretation about the following output correct???
mainthreadNO
Thread-0threadNO
mainwaitingjava.lang.IllegalThreadStateException
Thread-0waitingjava.lang.IllegalThreadStateException
interpretation
1.Thread abc runs its run method
2.Thread-0threadNO
3.since run is not synchronized,Jvm can select the main thread and move thread-0 to runnable and main running state
4.abc.run() is executed and
5.mainthreadNO is printed
6. start causes
mainwaitingjava.lang.IllegalThreadStateException
7.then control is given back to thread-0,it tooo sees the start
8.Thread-0waitingjava.lang.IllegalThreadStateException is printed
--------------------------------------------------------------------------------------
Query-4
So,it is impossible to predict the output of the code i posted ??
Query-5
can anyone answer my queries??
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
You can't expect the order of the output here! The main thread and the thread which you've created can run interchanging order! Please ask one your question per post, I don't mean per thread, but per post. And Simplify your questions! Then, you'll get more responses!
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
QueryA.
can this be the sequence for the code
1.first,abc.start() runs,so control goes to run method
2.Thread-0threadNO is printed
3.then,it sees start so,IllegalThreadStateException occurs
4.we get Thread-0waitingjava.lang.IllegalThreadStateException
5.then JVM,selects the main thread and executes abc.run();
6.main thread calls run method,
7.prints mainthreadNO
8.and it sees the start and then
mainwaitingjava.lang.IllegalThreadStateException prints,since main has already started
output:
Thread-0threadNO
Thread-0waitingjava.lang.IllegalThreadStateException
mainthreadNO
mainwaitingjava.lang.IllegalThreadStateException
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
QueryB.
is my interpretation about the following output correct???
mainthreadNO
Thread-0threadNO
mainwaitingjava.lang.IllegalThreadStateException
Thread-0waitingjava.lang.IllegalThreadStateException
interpretation
1.Thread abc runs its run method
2.Thread-0threadNO
3.since run is not synchronized,Jvm can select the main thread and move thread-0 to runnable and main running state
4.abc.run() is executed and
5.mainthreadNO is printed
6. start causes
mainwaitingjava.lang.IllegalThreadStateException
7.then control is given back to thread-0,it tooo sees the start
8.Thread-0waitingjava.lang.IllegalThreadStateException is printed
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
please HELP ME
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
mohitkumar gupta wrote:please HELP ME
The Java language gives you all the room you need to write code that would be very difficult for others to understand. Java also permits you to write code that is very easy to understand. Most development teams would prefer the latter.
Have a look on here and here.
As I said earlier, You can't expect the order! The main thread and the thread which you've created can run interchangeably.
|
 |
 |
|
|
subject: Callin start from run method In case of threads Problem
|
|
|