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(){
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){
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.
source share