This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Call to subclass method fails intermittently from thread  RSS feed

 
N Carlo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

My program works fine without concurrency, but when I introduce threads, it sometimes fails to produce any output. The code looks like this:



Note that the abstract base class Engine does not implement the turnOn method. Its concrete subclasses, such as CarEngine and TruckEngine do. It seems to me that the inner class Starter should call the outer class's turnOn method, which is implemented by the concrete subclasses, but again, sometimes it works and sometimes nothing happens. When it fails, I do not get an exception, I just get no output. What is going on here? Any and all help is greatly appreciated.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually when you have intermittent problems which only show up in multi-threaded environments, it's because your code isn't thread-safe. I think you're on a wild goose chase with that idea, and you should be looking at the actual code which is failing to act the way you think it should.
 
N Carlo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Paul. I will look more closely at that.
 
sarvesh meens
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Would like to add a few points to Paul's response.
For the snippet posted, taking a look at turnOn() method and checking for thread safety may provide some clues.
When the program hangs or when you see inconsistent behavior,investigating the thread dump may help.
Refer to this link, for ways to generate thread dump.

 
N Carlo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Sarvesh. After looking into it some more, I realized that the main application was (sometimes) terminating before the threads could complete their work. I added a call to Thread.join() so the main application waits for the threads to complete before exiting, and now it works fine. Thanks again for your help.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!