如何做一个多线程服务器的工作? [英] how does a multi-threaded server work?

查看:176
本文介绍了如何做一个多线程服务器的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从别人得到了与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 between isStopped() and onStop()....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 the accept() method will throw an exception. That's why there is a if(isStopped()) check in the code to avoid throwing an exception.

how it gets to make the connection between isStopped() and onStop()

isStopped() is a private method that tests to see if someone has called the stop() method. I assume onStop() is an Android method? They are not related although someone might want to call singleThreadedServer.stop() inside of onStop().

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 a volatile boolean or AtomicBoolean instead.

这篇关于如何做一个多线程服务器的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆