简体   繁体   English

如何保持多线程Java / Scala应用程序运行?

[英]How can I keep a multi-threaded Java/Scala application running?

I'm pretty new to Scala/Java and have to build a service that will have at least two sub-services running: a socket-based subscriber that listens for messages to kick off workers, and a web server that will serve a status page for those workers. 我对Scala / Java还是很陌生,必须构建一个至少运行两个子服务的服务:一个基于套接字的订户,侦听消息以启动工作程序,以及一个将为状态页提供服务的Web服务器对于那些工人。

I can get these things to run, but after both start the whole process immediately exits with code 0. 我可以运行这些东西,但是在两者都启动之后,整个过程将立即以代码0退出。

I did some research to learn about user threads vs daemon threads in Java, as well as threading in general, so now my approach is basically this: 我做了一些研究,以了解Java中的用户线程与守护程序线程以及一般的线程,因此现在我的方法基本上是这样的:

val webServerThread = new Thread(WebServer(config)).start()
val subscriberThread = new Thread(Subscriber(config)).start()
val aliveThread = new Thread(keepAlive(true)).start()

The third thread simply contains a while(true){} block to leave a user thread up. 第三个线程仅包含while(true){}块,以保留用户线程。

There has to be a smarter way of doing this, but I don't know what it is and seems impossible to discover. 必须有一种更聪明的方法来执行此操作,但是我不知道它是什么,而且似乎无法发现。 How do http server's stay running, for example? 例如,http服务器如何保持运行? Is there a while(true) loop underneath every framework out there? 每个框架下面是否有一个while(true)循环?

Any help would be appreciated. 任何帮助,将不胜感激。

The run() method of the thread would have to be an endless loop, until some condition occurs and you exit the loop. 线程的run()方法必须是一个无限循环,直到出现某种情况并退出循环为止。

To wait for a thread to exit, the way to do that is as follows: 要等待线程退出,执行方法如下:

final Runnable runnable = new Runnable() {
    public void run() {
        // do stuff
    }
};

final Thread thread = new Thread(runnable);
thread.start();
try {
    thread.join();
} catch (final InterruptedException e) {
    // deal with exception
}

Obviously, that will only wait for one thread. 显然,那只会等待一个线程。 It depends on your scenario whether this makes sense. 这是否有意义取决于您的方案。 Alternatively, you could use a ThreadPoolExecutor , invoke the shutdown() or shutdownNow() method, and use awaitTermination for it to stop. 另外,您可以使用ThreadPoolExecutor ,调用shutdown()shutdownNow()方法,并使用awaitTermination使其停止。

So for all services that have to stay running, say a web server or something, is there some code somewhere that just is basically while(shouldRun) {//nothing} ? 因此,对于所有必须保持运行的服务(例如Web服务器等),是否有一些代码基本上只是while(shouldRun) {//nothing}

No. There is never any reason to have a JRE thread that does nothing, and a thread that uses 100% CPU while accomplishing nothing would be even worse. 号从未有任何理由有JRE线程不执行任何操作,并使用100%的CPU同时实现什么会更糟一个线程。

Pretty much every thread in a program should sit in a loop, waiting for something to do .* Eg, An I/O thread that waits to receive and process input from some external source, a pool thread that waits for tasks to perform, a scheduler thread that waits until it's time to perform the next scheduled task. 几乎在一个程序中的每个线程应该坐在一个循环,等待事 。*例如,其等待接收,并从一些外部源,即等待任务执行池中的线程,进程输入的I / O线等待直到执行下一个计划任务的调度程序线程。

A web service must have at least one thread that sits in a loop and waits to handle incoming connections. Web服务必须至少有一个线程处于循环中并等待处理传入的连接。 I don't remember how to write that without doing some research first because there are so many open-source web servers out there: There's no reason to write your own except for practice. 我不记得在没有进行任何研究的情况下怎么写,因为那里有很多开源Web服务器:除了实践之外,没有理由编写自己的服务器。 There is even one built-in to the Oracle JRE.** In pseudo code, it might look like this: Oracle JRE甚至内置了一个。**在伪代码中,它可能看起来像这样:

while (! time_to_shut_down) {
    connection = WaitForIncomingConnection();
    clientThreadPool.handle(connection);
}

I can get these things to run, but after both start the whole process immediately exits with code 0. 我可以运行这些东西,但是在两者都启动之后,整个过程将立即以代码0退出。

I do not know why your program won't stay running. 我不知道为什么您的程序将无法继续运行。 I am not familiar with Scala or, with the WebServer class or the Subscriber class. 我不熟悉Scala或WebServer类或Subscriber类。

What is config ? 什么是config Maybe somebody would be able to help you if you would ammend your question to show how you create the configuration object. 如果您可以扩展问题以显示如何创建配置对象,也许有人可以为您提供帮助。


*One exception to that rule would be a compute-thread in a program that performs a single, massive computation and then exits. *该规则的一个例外是程序中执行单个大量计算然后退出的计算线程。

**See https://stackoverflow.com/a/3732328/801894 . **请参阅https://stackoverflow.com/a/3732328/801894 The server.start(); server.start(); call in that example is what kicks off the service thread. 该示例中的调用是启动服务线程的原因。 And, notice that the main() thread terminates right after it starts the server thread. 并且,请注意main()线程在启动服务器线程后立即终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM