多线程服务器如何工作?

发布于 2024-11-02 01:46:51 字数 1316 浏览 0 评论 0原文

我从某人那里得到了这个例子,在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

|煩躁 2024-11-09 01:46:51

你可能已经继续前进,但为了子孙后代......

当我在这一行出现异常时会发生什么

clientSocket = this.serverSocket.accept();

如果您在接受内部遇到异常,则不会返回套接字,因此无需关闭任何内容。 accept() 返回一个您应该处理的有效套接字,然后关闭它抛出异常。从来没有两者兼而有之。

在哪里调用stop()

Stop 看起来是由一些想要关闭服务器套接字的调用者从外部世界调用的。使用服务器,您创建一个服务器套接字,然后接受与远程客户端的单独连接,这会返回该客户端的套接字。每个客户端处理程序都需要关闭自己的连接。当服务器关闭时(在本例中,当调用 stop() 时),服务器套接字将关闭。

一旦调用 stop() ,服务器套接字就会关闭,并且 accept() 方法将抛出异常。这就是为什么代码中有一个 if(isStopped()) 检查以避免引发异常。

如何在 isStopped()onStop() 之间建立连接

isStopped() 是一个私有方法,用于测试某人是否有调用了 stop() 方法。我假设 onStop() 是一个 Android 方法?尽管有人可能想在 onStop() 内部调用 singleThreadedServer.stop(),但它们并不相关。

那个同步是什么?

多线程程序使用synchronized关键字作为互斥锁和内存屏障。它一次只允许 1 个线程在 synchronized 块内执行。它还确保所有局部变量在线程和中央内存之间同步。请参阅java文档:

http://download.oracle.com/javase/tutorial/ Essential/concurrency/syncmeth.html

在我看来,同步该私有方法确实很恶心。我认为这样做是为了确保 isStopped 已更新。我会使用 volatile 布尔值或 AtomicBoolean 来代替。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文