简体   繁体   English

Java - 有不同的启动线程的方法吗?

[英]Java - are there different ways of starting threads?

Ok, guys so my teacher uses this code to start a thread if a thread is not already active. 好吧,大家好,所以如果线程尚未激活,我的老师会使用此代码启动一个线程。 But i have been taught that to run threads no matter if its runnable or extending thread, you start it by the start method and not run. 但我已经被教导要运行线程,无论它是运行还是扩展线程,你都可以通过start方法启动它而不是运行。 But in this case he starts it with run, why is that? 但在这种情况下,他开始运行,为什么?

public void start2(Runnable r) {
    if (thread == null) {
        thread = new Thread(new Runnable() {
            public void run() {
                r.run();
                thread = null;
            }
        });
        thread.start();
    }
}

Your teacher starts thread with thread.start() . 你的老师用thread.start()启动线程。 He just implemented the runnable interface inside the Thread object initialization which is the absolutely correct approach. 他刚刚在Thread对象初始化中实现了runnable接口,这是绝对正确的方法。

A more modern approach would be to use an Executor to run the thread: 更现代的方法是使用Executor来运行线程:

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
    String threadName = Thread.currentThread().getName();
    System.out.println("Hello " + threadName);
});

You have a better control of the thread: 您可以更好地控制线程:

  • Can retrieve some result (with futures) 可以检索一些结果(带期货)
  • Know if your thread is over ( executor.isTerminated() ) 知道你的线程是否结束( executor.isTerminated()
  • Request/force a shutdown ( executor.awaitTermination() / executor.shutdownNow() ). 请求/强制关闭( executor.awaitTermination() / executor.shutdownNow() )。

These functionalities are not natively supported by the thread.start() that your teacher shows you (which is, by the way, a good way to launch a thread). 您的老师向您展示的thread.start()本身不支持这些功能(顺便说一下,这是启动线程的好方法)。

For more about Executors, I advice this excellent tutorial . 有关Executors的更多信息,我建议这个优秀的教程

The r.run(); r.run(); part in your code is just a method call to your Runnable r input parameter, which will be an implementation of the Runnable interface. 代码中的一部分只是对Runnable r输入参数的方法调用,该参数将是Runnable接口的一个实现。

This does not start a thread 这不会启动一个线程

It's just a method call that is executes the input's implementation of Run method. 它只是一个方法调用,它执行Run方法的输入实现。
It will execute just like any other method. 它将像任何其他方法一样执行。

Then, the actual thread will start at thread.start(); 然后,实际线程将从thread.start();开始thread.start();

Long answer: 答案很长:
What is happening here is, first checking the thread variable. 这里发生的是,首先检查线程变量。
If its null then initialize a new Thread with an anonymus class thread = new Thread(/*here --> */new Runnable() {.... and implementing the run() method. 如果为null,则使用anonymus类初始化一个新线程thread = new Thread(/*here --> */new Runnable() {....并实现run()方法。
Inside the run() there is a call, made to the outer method's input param, called Runnable r with r.run(); run()内部有一个调用,调用外部方法的输入参数,用r.run();调用Runnable r r.run(); then set the thread variable to null. 然后将线程变量设置为null。
Just outside of the if statement, the teacher starts the thread with thread.start(); if语句之外,教师使用thread.start();启动线程thread.start(); .

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

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