我有一个用 Java 编写的多线程回显服务器的简单代码(它将接收到的任何内容返回给客户端)。我正在分析服务器的各种资源,包括线程统计信息。以下是根据连接的客户端数量列出的一些统计数据。我的问题是与非基线相比的基线(# of clients 0)!
1) 为什么当单个客户端连接时,总线程数增加 2? (对于其余部分,增加 1 是有意义的)
2) 两个非守护线程是什么?!以及为什么守护进程最初增加 1 然后固定?
它们是随机的吗?!
# clients 0 1 2 3 4 5 6 7 8 9 10
Total Started Thread Count 15 18 19 20 21 22 23 24 25 26 27
Thread count 14 16 17 18 19 20 21 22 23 24 25
Peak thread count 14 16 17 18 19 20 21 22 23 24 25
Daemon thread count 12 13 13 13 13 13 13 13 13 13 13
这是服务器的一段代码。我同时使用 RMI(供客户端轮询消息)和服务器套接字(供客户端发送消息)。如果需要其他类(class),请告诉我。
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;
public class ServerRMI extends Thread implements Hello {
//centralized token manager runs polling server and socket server to receive updated tokens
static Vector<String> tokenList= new Vector<String>();
protected Socket clientSocket;
static int RMIRegistryPort=9001;
static int SocketServerPort=9010;
public static void main(String[] args) throws IOException {
try {
ServerRMI obj = new ServerRMI();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.createRegistry(RMIRegistryPort);
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
ServerSocket serverSocket = null;
//initialize token list
//A needs to execute first
tokenList.add(0,"0");
try {
serverSocket = new ServerSocket(SocketServerPort);
System.out.println("Connection Socket Created");
try {
while (true) {
System.out.println("Waiting for Connection");
new ServerRMI(serverSocket.accept());
}
} catch (IOException e) {
System.err.println("Accept failed.");
}
} catch (IOException e) {
System.err.println("Could not listen on port: "+SocketServerPort);
} finally {
try {
serverSocket.close();
} catch (IOException e) {
System.err.println("Could not close port: "+SocketServerPort);
}
}
}
private ServerRMI(Socket clientSoc) {
clientSocket = clientSoc;
start();
}
public ServerRMI() {}{
// TODO Auto-generated constructor stub
}
public void run() {
System.out.println("New Communication Thread Started");
try {
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
tokenList.add(0,inputLine);
System.out.println("Server received: " + inputLine);
// System.out.println(" ququ size: "+queue.size());
out.println(inputLine);
if (inputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
} catch (IOException e) {
System.err.println("Problem with Communication Server");
}
}
public String pollServer() {
if(!tokenList.isEmpty()){
String data = tokenList.get(0);
System.out.println("Poll data: "+data);
return data;
} else{
return tokenList.size()+"";
}
}
}
最佳答案
我不确定你用什么来接收这些连接,但通常处理 TCP 连接的框架是非阻塞的,比如 Netty使用主线程来监听端口和线程池来处理传入的连接。这意味着如果线程池限制为 1 个线程,它将至少为传入连接打开 2 个线程。
查看此 example来自 netty 页面,其中在引导服务器时使用了 2 个 NioEventLoopGroup。
这 2 个线程是不阻塞传入流量所必需的。
关于java - 守护进程线程数、线程数和总启动线程数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37154657/
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
一、引擎主循环UE版本:4.27一、引擎主循环的位置:Launch.cpp:GuardedMain函数二、、GuardedMain函数执行逻辑:1、EnginePreInit:加载大多数模块int32ErrorLevel=EnginePreInit(CmdLine);PreInit模块加载顺序:模块加载过程:(1)注册模块中定义的UObject,同时为每个类构造一个类默认对象(CDO,记录类的默认状态,作为模板用于子类实例创建)(2)调用模块的StartUpModule方法2、FEngineLoop::Init()1、检查Engine的配置文件找出使用了哪一个GameEngine类(UGame
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/