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

Stopping a thread  RSS feed

 
Marcus Rauchfuss
Ranch Hand
Posts: 51
1
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

since stop() is depreciated, how can I cleanly stop a thread? I tried putting a while(boolean==true) around the code inside run() but it does not have any effect. The thread just keeps running, even if I change the state of the boolean variable.
What other options do I have?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is extremely likely that your run method code is not working the way you think it is.

What debugging / logging checks are in your run method? Perhaps you should post your code.

Arranging for a graceful exit from the run method is the best way to "stop" a Thread.

Bill
 
Marcus Rauchfuss
Ranch Hand
Posts: 51
1
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Bill,


this is the run() method in question:




Exchanging while() with if() does not work either.

What graceful exit is possible?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that never exits, then the logical conclusion is that the method getStopperP() always returns 1.

A System.out message in getStopperP() showing the value it is about to return would be my next step.

If anything else is returned the while should terminate and the Thread return from the run()

Your handling of the InterruptedException is causing Java teachers everwhere to cringe.

Bill
 
Marcus Rauchfuss
Ranch Hand
Posts: 51
1
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you :-)

William Brogden wrote:
Your handling of the InterruptedException is causing Java teachers everwhere to cringe.

Bill


Luckily, this is not my handling. I am working on a homework assignment and most of the code you see (all except the while()) was provided ;)
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to this ...

Marcus Rauchfuss wrote:
Exchanging while() with if() does not work either.

This is saying that even one iteration is taking forever -- so, fixing the getStopperP() "issue" may not work.

Also, each iteration will take at least 20 seconds (assuming that the method calls are quick), as it is 10 times 2 seconds just for the sleep() calls, so you need to wait a bit longer than that to confirm if() does not work.

Henry
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably getStopperP and setStopperP (or boolean ? ) are synchronized or act on a variable declared volatile ?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!