• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Interrupt multiple threads on the same time  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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: 7
  • Mark post as helpful
  • send pies
  • 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: 62231
193
  • Mark post as helpful
  • send pies
  • 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: 7
  • Mark post as helpful
  • send pies
  • 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: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just a sample.
 
Campbell Ritchie
Marshal
Posts: 62231
193
  • Mark post as helpful
  • send pies
  • 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: 6363
437
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a beginner's topic, hence moved to Java in General and Swing forums.
 
R. Rusu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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: 21555
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Threads and Swing don't mix well. Try using a SwingWorker instead.
 
This will take every ounce of my mental strength! All for a tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!