   [java] Server with timeout.
  I'm trying to implement a Server which will timeout after a period of inactivity and stop replying. I have the following code, but it seems to use up my cpu and leaks memmory. Is there a better way to do this?//various importspublic class LoggingServer extends Thread{    private static final long IDLE_TIME_MILLIS = 15000;    private static final int LOGGING_SERVER_PORT = 9091;   private Collection<TraceLogData> log = Collections.synchronizedCollection(new ArrayList<TraceLogData>() );    private static ServerSocketChannel ssChannel;    private AtomicBoolean CallCompleate = new AtomicBoolean(false);   static {   try {  ssChannel = ServerSocketChannel.open();  ssChannel.configureBlocking(false);  ssChannel.socket().bind(new InetSocketAddress(LOGGING_SERVER_PORT));   } catch (IOException e) {}    }   public void run(){   try {  long lastConnect = System.currentTimeMillis();  while( !CallCompleate.get() || System.currentTimeMillis() < lastConnect + IDLE_TIME_MILLIS ){ SocketChannel sChannel = ssChannel.accept(); if(sChannel!=null){    LoggingConnectionHandler handler =    new LoggingConnectionHandler(sChannel.socket(), log);handler.start();lastConnect = System.currentTimeMillis(); } else {Thread.yield(); }  }   } catch (IOException e) {  e.printStackTrace();   }   return;    }    public boolean getCallCompleate() {   return CallCompleate.get();    }    public void setCallCompleate(boolean callCompleate) {   CallCompleate.set(callCompleate);    }   }Sorry about the mixed tabs and spaces.   
  Do not use Thread.yield(), use Thread.sleep() instead. There you can pass the number of milliseconds you want to wait, e.g. 100ms, or 500ms - depends on how much you want to "load" the server and how fast should be the response.   