• 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

Thread states example

 
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no RUNNING state.
An executing thread is (as your output shows) RUNNABLE.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic