java java线程类run()方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3035595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:02:52  来源:igfitidea点击:

java Thread class run() method

javamultithreading

提问by JavaUser

Thread class has run method to implement the business logic that could be executed in parallel.But I want implement different business logics in a single run method and to run simultaneously.How to get this feature.

Thread类有run方法来实现可以并行执行的业务逻辑。但是我想在一个run方法中实现不同的业务逻辑并同时运行。如何获得这个功能。

thanks

谢谢

回答by aioobe

That's right. You implement (or override) the run-method of a Thread to do stuff in parallell, however, there is an important difference when calling runvs calling start.

那就对了。您实现(或覆盖)runThread的-method 以并行执行操作,但是,调用run与调用start.

There is nothing special with the runmethod. Calling runwill behave just like any call to a regular method, and the control will not return to the caller until the runmethod has finished. The magic happens in start. When calling startthe control is returned to the calling side immediately, and a new thread is spawned that has the runmethod as entry point.

该方法没有什么特别之处run。调用的run行为就像对常规方法的任何调用一样,并且在run方法完成之前控件不会返回给调用者。魔法发生在start. 调用时start,控件立即返回到调用方,并生成一个以该run方法为入口点的新线程。

So, for example, if you want to execute two tasks simultaneously in different threads, you do something like:

因此,例如,如果您想在不同的线程中同时执行两个任务,您可以执行以下操作:

Thread t = new Thread() {
    public void run() {
        doTask1();
    }
};

// Spawn a new thread that doTask1. (don't call run here!)
t.start();
// Control returns immediately while thread t is busy with doTask1.

doTask2();

An example run:

运行示例:

    Thread t = new Thread() { 
        public void run() {
            try {
                Thread.sleep(1000);
                System.out.println("Slept for 1 second.");
            } catch (InterruptedException e) {
            }
        }
    };

    t.run();
    System.out.println("Returned from run.");

    t.start();
    System.out.println("Returned from start.");

Yields output

产量输出

                       (one second pause)
Slept for 1 second.
Returned from run.
Returned from start.
                       (one second pause)
Slept for 1 second.

回答by RonK

I think that the best course of action would be to have two separate threads.

我认为最好的做法是拥有两个独立的线程。

You can (and probably should) write a new class that implements Runnableand inside it place your logic. If there are common activities between the two business logics that you have to implement, you can use this class as a base class for the two "Runnables". Each Runnableshould be spawned in a separate thread.

您可以(并且可能应该)编写一个新类来实现Runnable并在其中放置您的逻辑。如果您必须实现的两个业务逻辑之间存在公共活动,则可以使用此类作为两个“Runnables”的基类。每个都Runnable应该在一个单独的线程中产生。

You can find very good reasoning for Thread vs. Runnableon this post: "implements Runnable" vs. "extends Thread"

你可以Thread vs. Runnable在这篇文章中找到很好的理由: “implements Runnable” vs. “extends Thread”

回答by Itay Karo

Make this run method spawn new thread with one logic and another thread with the second logic.

使这个 run 方法用一种逻辑生成新线程,用第二种逻辑生成另一个线程。