我有一个封装了服务器套接字的类,即服务器功能。
该类的接口是:
class Server{
public void start();
public void stop();
}
开始如下:
public void start(){
ExecutorService tp = Executors.newSingleThreadExecutor();
while(!stop){
try {
Socket clientConnection = serverSocket.accept();
tp.execute(new ClientProcessor(clientConnection));
} catch (IOException e) {
stop = true;
}
}
我很难弄清楚如何在不阻止我的main
.
我的意思是我需要从后台线程启动服务器,所以我想到了以下几点:
ExecutorService tp2 = Executors.newSingleThreadExecutor();
tp.submit(new Runnable(){
public void run(){
Server s = new Server();
s.start();
}
});
}
但我想知道服务器启动并且没有抛出异常。我怎样才能做到这一点?
即我如何从我的main
线程中知道后台服务器启动正常,所以我可以继续进行其余操作?