0

以下是我实现thrift服务器的方式,但在调用后它不会返回到 main therad serve()

public class ThriftServerRunner implements Runnable {
    private int thriftServerPort;
    public ThriftServerRunner(int thriftServerPort, LogWriter logWriter) {
        this.thriftServerPort = thriftServerPort;   
    }
    @Override
    public void run() {
        try {
            SetupThriftServer();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }               
    }

    private void SetupThriftServer() throws Exception {
    try {
            TServerSocket serverTransport = new TServerSocket(this.thriftServerPort);
            ThriftService.Processor<ThriftService.Iface> processor = new ThriftService.Processor(new ThriftServiceImpl());
            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
        server.serve();      
    } catch (TTransportException e) {
            e.printStackTrace();
    }
    }
}
4

1 回答 1

2

您确实调用start()启动线程(这将创建一个新线程,然后run()从这个新线程调用对象函数),而不是run()(它将仅从run()您当前所在的同一线程运行),对吗?

serve()不打算返回,除非您停止服务器(从另一个线程)。

于 2013-03-15T17:07:23.047 回答