I am not sure if there was a Post about it because "Suspend" gives a LOT of results in search, but I hope not.

1. My problem is: i am writing a small server application (very small, it is supposed to stay in the tray and do little to nothing) that of course has a few threads for connections. The problem is, each time the PC goes to suspend/hibernate, the threads are corrupted or something and the CPU usage goes up. So the idea was to close the connections and end threads when it happens, or at least after the system is back active.

Here is my connection listener:

Java Code:
public class InternetConnectionListener implements Runnable {
	ServerSocket serverSocket = null;
	InternetListener internetListener = null;
	List<InternetListener> internetListenerList = new ArrayList<InternetListener>();
	public InternetConnectionListener() {
		try {
			serverSocket = new ServerSocket(27782);
		} catch (IOException e) {
			e.printStackTrace();
			Main.log("Couldn't create socket");
		}
	}

	@Override
	public void run() {
		while(!serverSocket.isClosed()){
			Thread t;
			try {
				internetListener = new InternetListener(serverSocket.accept());
				t = new Thread(internetListener);
				t.start();	
				internetListenerList.add(internetListener);
				
				if(internetListenerList.size()>1){
					try {
						
						if (internetListenerList.get(internetListenerList.size() - 2).closeConnection() == 1){
							internetListenerList.remove(internetListenerList.size() - 2);
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				Main.log("SocketThread: " + internetListenerList.size());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Main.log("Couldn't accept connection");
			}
		}
		serverSocket = null;
		Main.log("InternetConnection Terminated");
	}
	
	void closeConnection(){
		for(InternetListener t: internetListenerList){
			try {
				t.closeConnection();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			serverSocket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
and my socket listener:

Java Code:
public class Listener implements Runnable {

	Profile profile;
	OutputStream out;
	InputStream in;
	
	@Override
	public void run() {

		try {
			Main.log(this.toString() + " <  Started");
			char c;
			while(true){
				StringBuilder strbuilder = new StringBuilder();
				c = (char) in.read();
				if(c == '#'){
					while(((c = (char) in.read())) != '~'){
						strbuilder.append(c);
					}
					if (parseAndDo(strbuilder.toString()) == -1) {
						break;
					}
					strbuilder.delete(0, strbuilder.length());
				}
			}
            Main.log(this.toString() + " <  Terminated");

		} 
		
		catch ( IOException ee ) {  
			
			Main.log(ee.toString() + "     List IO");
		}
	
	}

        ...
2. Btw, I also have a problem with "clean" terminating of connections. How do I close the "in.read()" if the mobile device (the client is on android) breaks of the connection (out of range, power of, whatever). It keeps waiting and waiting, but there isn't coming anything. The only thing i could thing of is terminating the thread after a new connections (there are not supposed to be more than on anyway).

3. In short: how do I keep those threads from taking my whole CPU after some time.

Big Thanks for any advise in advance :)