• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread states example

 
raja singh kumar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying out this sample program on thread states. I am seeing that the status of the thread is never in the "RUNNING" state. If a thread is inside the run method it should be in running state right?




Output:
Run by :Cat state is RUNNABLE
Run by :Cow state is RUNNABLE
End of Run method of :Cow
Run by : Dog state is RUNNABLE
End of Run method of : Dog
Current state of thread cat is RUNNABLE
End of Run method of :Cat
Current state of thread dog is TERMINATED
Current state of thread cow is TERMINATED
Current state of thread cat is TERMINATED
Current state of thread dog is TERMINATED
Current state of thread cow is TERMINATED
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no RUNNING state.
An executing thread is (as your output shows) RUNNABLE.
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One. Threads are susceptible to timing issues. Perhaps, it would be a good idea to check the state more than once? such as a tight loop that is checking the state for a few seconds, or even a minute?

Two. For threads to be in the running state, they actually have to be running stuff. So, perhaps, it would be good idea to make your animal threads run longer? or perhaps, it would be a good idea to give it something to do -- such as a long mathematical Fibonacci calculation?

Three. You definitely need to run this in a JVM / Operating System that has access to more than one core/processor. After all, one thread is definitely running -- ie. the main thread, as that is the currently running thread. So, you actually need hardware that allows for more than one currently running thread.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:There is no RUNNING state.
An executing thread is (as your output shows) RUNNABLE.


Wow. You are absolutely correct. I didn't even bother to check the docs. Sorry.

Have a cow.
Henry
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is an odd name.
I had to read the Thread.State thing a couple of times to make sure I hadn't missed something.
 
Campbell Ritchie
Sheriff
Posts: 51467
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you get to the print calls, the threads may or may not have finished whichever tasks they were supposed to do. You cannot therefore be sure they are running, but they are in a suitable state to be run, i.e. RUNNABLE.
 
raja singh kumar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no RUNNING state.

Please explain and clarify what you are saying. RUNNING is the most important state and you are saying it doesn't exist. Does a thread in runnable/running state show the thread state as runnable?
 
Paul Clapham
Sheriff
Posts: 21586
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:There is no RUNNING state.

Please explain and clarify what you are saying.


I don't know what there is to clarify. If you go to the API documentation and look at the values of the Thread.State enum, it's plain to see that there are six values of the enum. One of them is called RUNNABLE. None of them is called RUNNING. There is nothing unclear about that documentation. You might also pause a few minutes and read the descriptions of those enum values, as that might focus your mind on asking more productive questions.
 
raja singh kumar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone paste a diagram with the thread states...in all diagrams I have seen there is a running state which according to the java documentation does not exist as already discussed in this thread
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:Can someone paste a diagram with the thread states...in all diagrams I have seen there is a running state which according to the java documentation does not exist as already discussed in this thread


The Java documentation does *not* say that the running thread state "does not exist". The Java documentation merely says that the Java API does not provide the information to the application.

Keep in mind that Java threads are implemented on top of the native threading environment (or should all be by now). And there are a lot of possible native environments -- Windows, Linux, Solaris, etc. So, don't expect the implementation of thread states to be the same between all these environments, and certainly, don't expect Java's API to match all (or any) of these environments either.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic