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

Threads and calling methods on it explicitly

 
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
Please confirm this sentence, Whether it's correct or not. I got it from a web.

We can only invoke the start() or stop() methods, when the thread is in New thread state. Calling any method besides start() or stop() will cause an IllegalThreadStateException.



But, my opinion is:


Here, the threads will run sequentially, first thread one runs, when it exists, the thread two starts.


Here, the start() method call run() asynchronously (doesn't wait for any result, just fire up an action), while we run run() method synchronously. we wait when it quits and only we can run the next line of our code.

Please correct, if I wrong! Thanks in Advanced!
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:Please confirm this sentence, Whether it's correct or not. I got it from a web.

We can only invoke the start() or stop() methods, when the thread is in New thread state. Calling any method besides start() or stop() will cause an IllegalThreadStateException.




well the statement is correct regarding start() method lets say if a thread is already in running state, if you again call start() it will cause IllegalThreadStateException for sure....
but I'm not sure about stop give a try your self write a piece of code and test....


hth
 
Sheriff
Posts: 9703
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. The Thread.stop method has been deprecated, so you don't have to worry about that for the exam.
2. calling run method directly doesn't start a new thread, so the call is synchronous, calling the start method starts a new thread and invokes the run method asynchronously...
 
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
Hi, So we can invoked any method rather than start() and stop() on a new thread. It's ok, that calling run() method on threads don't start a thread of execution. That's fine.
My question is What is the purpose of the IllegalThreadStateException in java?

Can we call any methods on a Runnable thread as like new Thread. Please clarify this. Thanks in Advanced!
 
Ankit Garg
Sheriff
Posts: 9703
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My question is What is the purpose of the IllegalThreadStateException in java?


It is thrown when you perform any action on a thread object which is not allowed because of the current state of the thread. Like if you call the start method on a thread twice, you'll get an IllegalThreadStateException as a thread can only be started once...
 
reply
    Bookmark Topic Watch Topic
  • New Topic