公司网站建设网站优化网络推广,网站改版什么意思,网站建设国内公司,男女做那个网站thrift作为一个从底到上除去业务逻辑代码#xff0c;可以生成多种语言客户端以及服务器代码#xff0c;涵盖了网络#xff0c;IO#xff0c;进程#xff0c;线程管理的框架#xff0c;着实庞大#xff0c;不过它层次清晰#xff0c;4层每层解决不同的问题#xff0c;可…thrift作为一个从底到上除去业务逻辑代码可以生成多种语言客户端以及服务器代码涵盖了网络IO进程线程管理的框架着实庞大不过它层次清晰4层每层解决不同的问题可以按需取用相当方便。 -------------------------------------------
| Server | -- 服务器进程调度
| (single-threaded, event-driven etc) |
-------------------------------------------
| Processor | -- RPC接口处理函数分发IDL定义接口的实现将挂接到这里面
| (compiler generated) |
-------------------------------------------
| Protocol | -- 协议
| (JSON, compact etc) |
-------------------------------------------
| Transport | -- 网络传输
| (raw TCP, HTTP etc) |
------------------------------------------- 其实对于服务端编程的技术大牛来说服务器调度可能最能体现个人技术功底但是从传输层到序列化这层的工作确实是比较繁琐工作可以直接利用thrift生成的代码来完成问题。 以上为题外话在thrift的java代码实现Server这一层有个TThreadPoolServer里面对于线程管理就是使用ThreadPoolExecutor下面贴下核心代码 public void serve() {try {serverTransport_.listen();} catch (TTransportException ttx) {LOGGER.error(Error occurred during listening., ttx);return;}stopped_ false;while (!stopped_) {int failureCount 0;try {TTransport client serverTransport_.accept();WorkerProcess wp new WorkerProcess(client);executorService_.execute(wp);//这个就是ThreadPoolExecutor} catch (TTransportException ttx) {if (!stopped_) {failureCount;LOGGER.warn(Transport error occurred during acceptance of message., ttx);}}}executorService_.shutdown();// Loop until awaitTermination finally does return without a interrupted// exception. If we dont do this, then well shut down prematurely. We want// to let the executorService clear its task queue, closing client sockets// appropriately.long timeoutMS options_.stopTimeoutUnit.toMillis(options_.stopTimeoutVal);long now System.currentTimeMillis();while (timeoutMS 0) {try {executorService_.awaitTermination(timeoutMS, TimeUnit.MILLISECONDS);break;} catch (InterruptedException ix) {long newnow System.currentTimeMillis();timeoutMS - (newnow - now);now newnow;}}} 值得注意的是在执行完所有任务的时候需要调用shutdown()方法这个在网上的很多例子都有但是对于最后一段作者反复检查状态再退出这个着实没有必要的在shutdown()方法中就有类似的代码了jdk1.7);再者java并不会在主线程退出的情况下会对其他线程造成影响所以这段代码更显多余:-D转载于:https://www.cnblogs.com/elvinni/p/4162982.html