• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stoping a Thread when is not needed anymore

 
Olivier López
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody¡


I was looking in the forum how to solve my problem but wasnt able to find the answer, so here i go:

I have these clases:

*hilo <-- this is the thread
*inter <--- this is the Form class with a button "SwitchForm", that when clicked starts the thread, and switches to form "secondForm"
*secondForm <-- another Form class with a button named "returnToInterForm"

When i press the button "SwitchForm" in the class inter, the thread "hilo" starts running, the form "inter" closes, and the form "secondForm" sets visible... this is the code in the button "SwitchForm" :



when i press the button "returnToInterForm" thats switches to "inter" again , i dont need the thread running anymore... but i dont know how to close that Thread.

I could write a code in the button "returnToInterForm" that let me close the thread...but dont know exactly how.

The way i found is this:



I Tried with a method in the Thread "hilo" like this:



and tried to activate that method using the code "hilo.closeHilo();" but have some problems, cause i cannot use that line of code if the method closeHilo is not static, and cannot make "public static void closeHilo()" cause "this.stop()" cannot be referenced in a static method.

I appreciate your help in advance¡¡


 
Istvan Kovacs
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use Thread.stop(), it's not safe - that's why it's deprecated.
To terminate:
- set a volatile boolean condition, check it in your Thread's loop, or
- interrupt() the Thread, check interrupted() from the loop. Calling interrupt() will allow you to wake up from wait() and sleep(), too.
- if the Thread you want to exit is blocked in I/O, often you'll need to close the I/O object (e.g. socket)

You could also use a single thread executor, and call its shutdown() method.

Java Concurrency in Practice by Brian Goetz et al. has a long, detailed description of termination, check it out.
 
Olivier López
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XD, thanks for the answer, but a little late XD.

As i said, where not able to change the boolean value for some reason, but now the problem is solved
 
Istvan Kovacs
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should check your code, and make sure you understand when you're dealing with an instance (non-static methods, where you can access 'this'), and when with the class (static methods and fields).
 
Olivier López
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Istvan Kovacs wrote:I think you should check your code, and make sure you understand when you're dealing with an instance (non-static methods, where you can access 'this'), and when with the class (static methods and fields).


Right. My problem was with the static stuff in the thread. But now is fixed
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!