ExecutorService pool = Executors.newFixedThreadPool(10);
表示新建了一個線程池,線程池里面有10個線程為任務隊列服務。
使用ServerSocket對象來初始化監聽端口。
PRivate static final int PORT = 19527; serverListenSocket = new ServerSocket(PORT); serverListenSocket.setReuseAddress(true); serverListenSocket.setReuseAddress(true);
private static ReentrantLock lock = new ReentrantLock (); private static int count = 0; private int getCount(){ int ret = 0; try{ lock.lock(); ret = count; }finally{ lock.unlock(); } return ret; } private void increaseCount(){ try{ lock.lock(); ++count; }finally{ lock.unlock(); } }
服務線程在開始給客戶端打印一個歡迎信息,
increaseCount(); int curCount = getCount(); helloString = "hello, id = " + curCount+"/r/n"; dos = new DataOutputStream(connectedSocket.getOutputStream()); dos.write(helloString.getBytes());