如何做一个多线程服务器的工作? [英] how does a multi-threaded server work?
问题描述
我从别人得到了与Android上的多线程服务器这个例子:
的http://tutorials.jenkov.com/java-multithreaded-servers/singlethreaded-server.html
但我理解code的一部分的一些困难:
而(!isStopped()){
插座ClientSocket的= NULL;
尝试{
ClientSocket的= this.serverSocket.accept();
}赶上(IOException异常五){
如果(isStopped()){
的System.out.println(服务器停止。);
返回;
}
抛出新的RuntimeException(错误接受客户端连接,E);
}
我不明白的是,当我在这行的异常会发生什么:
ClientSocket的= this.serverSocket.accept();
从我可以告诉的是,这个函数被调用:
私人同步布尔isStopped(){
返回this.isStopped;
}
但它是如何获取到关闭套接字?因为如果u得到的异常在试图接受一个客户,你应该附近接受返回的插座。
我认为这是在这里完成:
公共同步无效停止(){
this.isStopped = TRUE;
尝试{
this.serverSocket.close();
}赶上(IOException异常五){
抛出新的RuntimeException(错误关闭服务器,E);
}
}
但哪一个停止()
调用,它是如何得做出之间的连接isStopped()
和的onStop()
....什么是与同步?
希望我已经与我不清楚的地方清楚!感谢ü提前:)
您可能已经移动了,但为后人...
什么时,我有一个例外发生在这一行
块引用>ClientSocket的= this.serverSocket.accept();
如果你得到一个异常里面接受,则返回没有插座因此没有关闭。无论是
接受()
返回你应该处理,然后关闭的或的它抛出一个异常有效的套接字。不可能兼顾。
在哪里
停止()
叫什么名字?
块引用>停止看起来由想要关闭服务器套接字一些呼叫者必须从外界调用。与服务器,您可以创建一个服务器套接字,然后你接受远程客户端,它返回客户端套接字单个连接。每个客户端的处理程序需要关闭自己的个人联系。当服务器关闭(在这种情况下,当
停止()
的叫法),服务器插槽,然后关闭。在
停止()
被称为然后在服务器关闭套接字和接受()
方法将引发例外。这就是为什么有一个如果(isStopped())
检查在code,以避免抛出异常。
如何得做出
之间的连接isStopped()
和的onStop()
块引用>
isStopped()
是测试,看看是否有人叫停止()
方法的私有方法。我假设的onStop()
是一个Android的方法?他们是不相关的,尽管有人可能会想打电话给singleThreadedServer.stop()
里面的的onStop()
。
什么是与
同步
?
块引用>synchronized关键字是多线程程序的互斥和内存屏障使用。只允许1线程在同一时间内执行
同步
区块内。这也可以确保所有本地变量都线程和中央存储之间同步。请参阅Java文档:
的http://download.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
块引用>拥有私人的方法进行同步真毛海事组织。我认为这是正在做是为了确保
isStopped
已更新。我会用一个挥发性
布尔或的AtomicBoolean
代替。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 betweenisStopped()
andonStop()
....And what is with that "synchronized"?Hope I've been clear with my unclarities! Thank u in advance:)
解决方案You've probably moved on but for posterity...
what happens when I have an exception at this line
clientSocket = this.serverSocket.accept();
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.where is
stop()
called?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.how it gets to make the connection between
isStopped()
andonStop()
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()
.what is with that
synchronized
?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:http://download.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
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.这篇关于如何做一个多线程服务器的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!