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

Stoping a Thread when is not needed anymore

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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¡¡


 
Ranch Hand
Posts: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic