Java how to stop a thread if the program was closed

I get some kind of exception, and I need to know when the program closes, because I need to close the socket.

I have a default public static main method in which I repeat the action and the Thread class.

private static Thread thread; public static boolean isRunning = true; public static void main(String[] args){ thread = new Thread(new ThreadListenServer()); thread.start(); Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run(){ // some action } } timer.scheduleAtFixedRate(task, 0, 10000); isRunning = false; } 

And a thread class that runs in the background:

 public class ThreadListenServer implements Runnable{ private DatagramSocket socket; public ThreadListenServer() throws SocketException{ socket = new DatagramSocket(6655); } @Override public void run() { while(MainProgram.isRunning){ // some action } socket.close(); } } 

I do not know why, but isRunning it becomes false, but it should not. How should I close the socket if the main program was closed? (This is because Thread is still running in the background, even if the program was closed).

I was thinking of creating a socket in the main class, then I pass the socket object as a ThreadClass parameter, and if the program is closed, I must also close the socket.

+4
source share
4 answers

Using:

 thread.setDaemon(true); 

This will close the stream. It tells the JVM that it is a background thread, so it will be closed when exiting.

+4
source

I suppose you have some kind of JFrame working as a MainProgram class. You have 2 options

1: set Jframe to close all threads when it is closed.

 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

2: add a window listener and manually close your stream (maybe you need to send some information through the socket before closing it).

 addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { // send your socket its close message and shut everything down System.exit(0); } }); 
+2
source

A few things come to mind.

  • It would seem that you are performing an I / O blocking operation using sockets. You may need to interrupt either the current thread and / or socket so that it blocks the lock
  • You must set the stream as a daemon stream before starting it using setDaemon(true) . This will allow the JVM to automatically terminate the thread ...
  • isRunning should be marked volatile or use AtomicBoolean instead
+1
source

To stop all threads when your program exits cleanly , you need to define a termination policy for each thread that starts. This is usually done using the Interrupts and ExecutorService.shutdownNow() methods, which send an interrupt to each running thread.

The net completion policy consists of two parts:

  • Sending a stop signal to a stream - aka interrupting it
  • Designing Threads for Interruption

A thread in Java can be interrupted by calling the Thread.interrupt() method. Threads can check for interrupt by calling the Thread.isInterrupted() method. A good thread should check for interruption at regular intervals, for example. as a condition of the loop and checking the lock functions for InterruptedExceptions .

It is important to note that Socket in Java does not pay attention to interruption. For example, if a thread is blocked on Socket.accept() , it will not throw an InterruptedException when the thread is interrupted. In this case, you need to define a public method that closes the underlying socket by calling Socket.close() , causing the lock function to throw an exception (I think SocketException ).

+1
source

Source: https://habr.com/ru/post/1479172/


All Articles