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());