• 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

Calling methods before start()

 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A/C to Sun Java2 Tutorial,


When a thread is in New Thread state, you can only start the thread. Calling any method besides start when a thread is in this state makes no sense and causes an IllegalThreadStateException.


Anybody can give code based e.g.?
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin: correct this, if wrong...
We will get IllegalThreadStateException only in two cases.
1) When we call start() on already started thread
2) When we call setDaemon() after the thread is started
Uma
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uma,
1. true
2. not exactly started but "active", but I think it's the same!
3. also when we invoke countStackFrames is the thread is not suspended, but don;t worry that method is deprecated...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 11, 2001).]
 
Uma Viswanathan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Val: Thanks for your reply.
That exception will be thrown if we call setDaemon()when the thread is active...this is proper wording...
but I thought that a thread is active after it is started. That's why, i phrased the words like that.
OK. Can a thread be inactive, after it is started? If so, please explain val...
Is the thread inactive if it is waiting or blocking or suspended...?
Uma
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it depends on what you call an active thread:
Here is a quote from Java Threads (O'Reilly):


When is a Thread active? At first glance, this seems to be a simple question. Using the isAlive() method, a thread is considered alive during the period between the call to the start() method and a short time period after the stop() method is called. We might consider a threada ctive if it is alive.
However, if the definition of an active thread is a thread whose thread reference appears in the active count returned by the activeCount() method, we would have a different definition of active. A thread reference first appears in the thread array returned by the enumerate() method, and is counted by the activeCount() method, when the thread object is first constructed and not when the thread is started.
The thread is removed from the thread array either when the thread is stopped or when the run() method has completed. This means that if a thread object is constructed but is not started, the thread object will not be removed from the enumeration list, even if the original reference to the object is lost.


HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Uma Viswanathan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setDaemon() throws that exception if called before a thread is started
Thread t = new Thread();
t.start();
t.setDeamon( true ); //throws exception
Thread t = new Thread();
t.setDaemon();
t.start(); //does not throw exception
Java API says that we need to call setDaemon() before the thread is started. Also it says that it will throw that exception if the thread is active.
According to the above code and API, a thread is active only when it is started (but not just being creating the Thread object)
So, the definition from Java Threads (O'Reilly) and also activeCount() method "contradicts with" JAVA API. i.e, If a thread is considered to be active as soon as it is constructed (even before it is started)...
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Vikrama Sanjeeva
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A thread reference first appears in the thread array returned by the enumerate() method, and is counted by the activeCount() method, when the thread object is first constructed and not when the thread is started.
This para of O'Reilly says that Thread is even active when before start() is call.But i think a/c to cert. we have to consider the active thread as said by this para of O'Reilly


When is a Thread active? At first glance, this seems to be a simple question.Using the isAlive() method, a thread is considered alive during the period between the call to the start() method and a short time period after the stop() method is called.


A/C to me this para defines the correct state of Thread a/c to cert. point of view.
Bye.
Viki.

------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Uma Viswanathan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marilyn.
It clears my doubt (I think)...
So, the O'Reilly explanation is correct...
1) A thread is alive between the call to the start() method and a short time period after the stop() method is called.
2) A thread is active between the period (when the thread object is first constructed and not when the thread is started) and (either when the thread is stopped or when the run() method has
completed).
Need a change in the API for setDaemon():
They should specify this method will throw the exception if called when a thread is alive. THEY SHOULD NOT TELL THAT THIS EXCPETION WILL BE THROWN WHEN THE THREAD IS ACTIVE...YOUR CODE CONFIRMS THIS...
 
Our first order of business must be this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic