日期:2014-05-20 浏览次数:20955 次
public void run() { while (true) { try { Socket socket = server.accept(); synchronized (SocketDBServer.lock) { socketMap.put(socket.getInetAddress().toString().substring(1), socket);//保存客户端IP } if(AutoMain.switchFlag) { synchronized (AutoMain.obj) { if (socketMap.size() >= BaseConfig.MIN_CONTROLSER_COUNT) { AutoMain.switchFlag = false; AutoMain.obj.notify(); } } } SocketDBServerThread dbserver = new SocketDBServerThread(socket); logger.info("new SocketDBServerThread(socket)"); new Thread(dbserver).start(); logger.info("new Thread(dbserver).start();"); } catch (IOException e) { logger.error("SocketDBServer.run() server.accept()", e); } } }
//线程延时器,比while(true)性能好 ScheduledExecutorService scheduled = Executors.newSingleThreadScheduledExecutor(); ScheduledFuture future = scheduled.scheduleWithFixedDelay(this, 0, 100,TimeUnit.SECONDS); //线程池,可有效管理线程。 ExecutorService executors = new ThreadPoolExecutor(20, 20, 0L, TimeUnit.MILLISECONDS, new DefaultedOfferTimeoutBlockingQueue<Runnable>(5, 86400,TimeUnit.SECONDS)); executors.execute(new TaskThread());