gpt4 book ai didi

java - ReSTLet 服务器的行为类似于单线程

转载 作者:太空宇宙 更新时间:2023-11-04 07:06:21 25 4
gpt4 key购买 nike

我得到了以下代码作为 ReSTLet 的 hello world 测试

public static void main(String[] args) throws Exception {
//Find a way to get these from the ARGS...
Settings.setCurrent(new Settings());

// Create a new Restlet component and add a HTTP server connector to it

component.getServers().add(Protocol.HTTP, 8182);

component.getContext().getParameters().add("maxThreads", "512");
component.getContext().getParameters().add("minThreads", "100");

component.getDefaultHost().attach("/findMissingPackages", Jeblet.class);

// Now, let's start the component!
// Note that the HTTP server connector is also automatically started.
component.start();
}

@Get
public String toString() {
try {
Thread.sleep(10000);
}
catch(Exception ex) { }
String settingString = "stuff";
return settingString;
}

我遇到的问题是,如果我在 Chrome 中打开两个选项卡并连续访问服务器两次,则需要 20 秒才能在第二个选项卡上获得响应。这两个选项卡都需要 10 秒。

当我调试时,我只有一个调度程序。我如何告诉reSTLet我想要多个线程?

最佳答案

打开新的浏览器选项卡(或窗口)与打开新连接不同。浏览器非常擅长重用已经打开的连接,20 秒的延迟就是证明。您可以通过打印服务器中的远程 IP + 端口来验证这一点,这对于两个请求都是相同的。

在 Firefox 中,您可以通过按 ctrl+F5 强制建立新连接,Chrome 可能也有类似的功能。但是您也可以编写一个执行 get-request 的小型(多线程)客户端程序:编写起来并不困难,并且当您需要测试/调试服务器的其他功能时会派上用场。

关于java - ReSTLet 服务器的行为类似于单线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21318558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com