• 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

Interrupt multiple threads on the same time

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 3 threads on a frame and when I press the "Exit" button I want o stop the threads which are currently running and after that to close the frame with the program. For that I created an array where I have all the Threads of the frame and when the button "Exit" is pressed the program iterates over the array and if is there any thread running I interrupt it. The problem with my approch is that the program stops only 1 thread, not all of them. So, even though the frame is closed, there will be 2 threads running on the background.

Here is my program:

Interrupt multiple threads on the same time
 
R. Rusu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the problem. I don't have to check if the thread is running. What do you thing about this approch? Is it a safe one?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rusu Raducu wrote:. . . . What do you thing about this approch? Is it a safe one?

Please explain what you are doing, but you appear to be running three threads accessing the same thread‑unsafe objects simultaneously.
Swing® is not thread‑safe and all changes should be made on the event dispatch thread. What do the three threads do? How are you adding thread (line 39)? The add() method doesn't take a Thread parameter; please explain what those objects are.

It feels all wrong, I am afraid.
 
R. Rusu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All three threads does the same thing. I use the to import some data to a database. I use 3 threads for multiple import. I want to stop them when I quit the program and I want to add a dialog box(yes / no) where the the user can choose if he wants to interrup the running threads or not.

Thank you!
 
R. Rusu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is just a sample.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and at the time of interruption, you don't know whether the writing to the database has been completed?
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a beginner's topic, hence moved to Java in General and Swing forums.
 
R. Rusu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No! I will just check if threads are running. If they are running I will suppose that they haven't finished the upload and I will ask the user to answer if he really wants to interrupt the thread. I will display a dialog box with yes/no for each running thread.

PS: Why should I syncronize them? They don't upload the same file... They upload different filles...
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Threads and Swing don't mix well. Try using a SwingWorker instead.
 
reply
    Bookmark Topic Watch Topic
  • New Topic