为什么不在构造函数中启动线程?如何终止? [英] Why not to start a thread in the constructor? How to terminate?

查看:64
本文介绍了为什么不在构造函数中启动线程?如何终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在 Java 中使用线程.我写了一个类来实现 Runnable 并发运行到另一个线程.主线程负责监听串口,而第二个线程负责将数据发送到同一个端口.

I am learning how to use threads in Java. And I wrote a class that implements Runnable to run concurrently to another thread. The main thread handles listening to the serial port where as the second thread will handle sending data to that same port.

public class MyNewThread implements Runnable {
    Thread t;

    MyNewThread() {
        t = new Thread (this, "Data Thread");
        t.start();
    }

    public void run()  {
        // New Thread code here 
    }

第一个线程像这样开始第二个:

There first thread starts the second like this:

public class Main {
    public static void main(String[] args) throws Exception{
        new MyNewThread();
        // First thread code there
    }  
}

这可行,但我的编译器标记了一条警告:在构造函数中启动新线程是危险的.这是为什么?

This works but my complier flags a warning saying: It is dangerous to start a new thread in the constructor. Why is this?

这个问题的第二部分是:如果我在一个线程(串行端口监听线程)中运行一个循环并且我在我的第二个线程中键入一个退出命令.如何让第一个线程终止?谢谢.

The second part to this question is: how if I have a loop running in one thread (the serial port listen thread) and I type an exit command in my second thread. How do I get the first thread to terminate? Thanks.

推荐答案

对于您的第一个问题:在传入 this 的构造函数中启动线程会转义 this.这意味着您实际上是在完全构造对象之前发出对对象的引用.该线程将在您的构造函数完成之前启动.这可能会导致各种奇怪的行为.

To your first question: Starting a thread in a constructor passing in this escapes this. That means that you are actually giving out a reference to your object before it is fully constructed. The thread will start before your constructor finishes. This can result in all kinds of weird behaviors.

对于您的第二个问题:在 Java 中没有可接受的方法来强制另一个线程停止,因此您将使用一个变量,该线程将检查该变量以了解它是否应该停止.另一个线程会将其设置为指示第一个线程将停止.该变量必须是 volatile 或所有访问同步以确保正确发布.这是一些类似于您想要的代码.

To your second question: There is no acceptable way to force another thread to stop in Java, so you would use a variable which the thread would check to know whether or not it should stop. The other thread would set it to indicate that the first thread would stop. The variable has to be volatile or all accesses synchronized to ensure proper publication. Here is some code which would be something like what you want.

public class MyNewThread implements Runnable {

    private final Thread t;
    private volatile boolean shouldStop = false;

    MyNewThread() {
        t = new Thread (this, "Data Thread");
    }

    public void start() {
        t.start();
    }

    public void stop() {   
         shouldStop = true;
    }

    public void run()  {
         while(!shouldStop)
         {
             // do stuff
         }
    }
}

任何想要创建和启动线程的事情都可以:

Whatever wants to create and start the thread would do:

MyNewThread thread = new MyNewThread();
thread.start();

任何想停止线程的事情都会做:

Whatever wants to stop the thread would do:

thread.stop();

这篇关于为什么不在构造函数中启动线程?如何终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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