多线程服务器如何工作?
我从某人那里得到了这个例子,在android上有一个多线程服务器:
http: //tutorials.jenkov.com/java-multithreaded-servers/singlethreaded-server.html
但是我在理解部分代码时遇到了一些困难:
while(! isStopped()) {
Socket clientSocket = null;
try {
clientSocket = this.serverSocket.accept();
} catch (IOException e) {
if (isStopped()) {
System.out.println("Server Stopped.") ;
return;
}
throw new RuntimeException("Error accepting client connection", e);
}
我不明白的是,当我有这一行有一个例外:
clientSocket = this.serverSocket.accept();
据我所知,该函数被调用:
private synchronized boolean isStopped() {
return this.isStopped;
}
但是它如何关闭该套接字?因为如果你在尝试接受客户端时遇到异常,你应该关闭accept返回的套接字。
我假设这是在这里完成的:
public synchronized void stop() {
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
但是 stop()
在哪里被调用,如何在 isStopped()
和 onStop()< 之间建立连接/code>...“同步”是什么?
希望我已经澄清了我的不清楚之处!提前谢谢你:)
I've got from someone this example with a multithreaded server on android:
http://tutorials.jenkov.com/java-multithreaded-servers/singlethreaded-server.html
But I have a few difficulties in understanding a part of the code:
while(! isStopped()) {
Socket clientSocket = null;
try {
clientSocket = this.serverSocket.accept();
} catch (IOException e) {
if (isStopped()) {
System.out.println("Server Stopped.") ;
return;
}
throw new RuntimeException("Error accepting client connection", e);
}
What I don't understand is, what happens when I have an exception at this line:
clientSocket = this.serverSocket.accept();
From what I can tell is that this function gets called:
private synchronized boolean isStopped() {
return this.isStopped;
}
But how it gets to close that socket? Cause if u get exception in the attempt to accept a client you should close the socket returned by accept.
I assume that this is done in here:
public synchronized void stop() {
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
But where is stop()
called,how it gets to make the connection between isStopped()
and onStop()
....And what is with that "synchronized"?
Hope I've been clear with my unclarities! Thank u in advance:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可能已经继续前进,但为了子孙后代......
如果您在接受内部遇到异常,则不会返回套接字,因此无需关闭任何内容。
accept()
返回一个您应该处理的有效套接字,然后关闭或它抛出异常。从来没有两者兼而有之。Stop 看起来是由一些想要关闭服务器套接字的调用者从外部世界调用的。使用服务器,您创建一个服务器套接字,然后接受与远程客户端的单独连接,这会返回该客户端的套接字。每个客户端处理程序都需要关闭自己的连接。当服务器关闭时(在本例中,当调用
stop()
时),服务器套接字将关闭。一旦调用
stop()
,服务器套接字就会关闭,并且accept()
方法将抛出异常。这就是为什么代码中有一个if(isStopped())
检查以避免引发异常。isStopped()
是一个私有方法,用于测试某人是否有调用了stop()
方法。我假设onStop()
是一个 Android 方法?尽管有人可能想在onStop()
内部调用singleThreadedServer.stop()
,但它们并不相关。多线程程序使用synchronized关键字作为互斥锁和内存屏障。它一次只允许 1 个线程在
synchronized
块内执行。它还确保所有局部变量在线程和中央内存之间同步。请参阅java文档:在我看来,同步该私有方法确实很恶心。我认为这样做是为了确保 isStopped 已更新。我会使用
volatile
布尔值或AtomicBoolean
来代替。You've probably moved on but for posterity...
If you get an exception inside of accept then no socket is returned so there is nothing to close. Either
accept()
returns a valid socket that you should handle and then close or it throws an exception. Never both.Stop looks to be called from the outside world by some caller that wants to shutdown the server socket. With a server, you create a server-socket and then you accept individual connections to remote clients, which returns a socket for that client. Each of the client handlers need to close their own individual connections. When the server is shutting down (in this case when
stop()
is called), the server-socket is then closed.Once
stop()
is called then the server socket is closed and theaccept()
method will throw an exception. That's why there is aif(isStopped())
check in the code to avoid throwing an exception.isStopped()
is a private method that tests to see if someone has called thestop()
method. I assumeonStop()
is an Android method? They are not related although someone might want to callsingleThreadedServer.stop()
inside ofonStop()
.The synchronized keyword is used by multithreaded programs as a mutex and memory barrier. It only allows 1 thread to be executing inside of the
synchronized
block at one time. It also makes sure that all of the local variables are synced between the threads and central memory. See the java docs:Having that private method be synchronized is really gross IMO. I assume this is being done to make sure
isStopped
has been updated. I would have used avolatile
boolean orAtomicBoolean
instead.