• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Callin start from run method In case of threads Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you expect? You can't start a thread more than once!
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please HELP ME

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic