Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread - Concurrency  RSS feed

 
Nageswar Kakolla
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

Problem Statement : I am adding 3 Object Instances of FCS to ArrayList and
executing them in run() of BoomerangServiceTest. FCS in turn extends from Thread which when start, FCS will execute run.

Same thing happens repeatedly after some Interval which is scheduled by Timertask.

Unfortunately, Thread control is not coming back after FCS is executing 3rd time.

Does Thread expires after run() is executed or we need to stop() explicitly

[ added [url=http://faq.javaranch.com/view?UseCodeTags]code tags - Jim ]
[ January 25, 2007: Message edited by: Jim Yingst ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the code you've given here wouldn't quite compile; there are little bits and pieces of extra text here and there, and missing methods like getInstance(), and a lot of likely non-fatal mistakes as well (passing 'i' to the FCS constructor in the loop, for example.) It's hard to say precisely what you expect to happen when you're not showing us the whole code.

But one thing you might not know is that you can call start() on a Thread just once. Once a thread has been started, it can never be "re-started"; when it's done, it has to be just thrown out.
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nageswar Kakolla:

Does Thread expires after run() is executed or we need to stop() explicitly


Note that in general you should not be calling the stop() method on a thread. The Java API discusses why that method is unsafe and why it should be avoided.

Also, on a side note. You may also want to avoid variable "shadowing" where you have local variables named the same as your member variables. In a small example, it's not too hard to follow, but this can certainly lead to confusion and problems as your code grows.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!