为什么不在构造函数中启动一个线程?如何终止?

我正在学习如何在Java中使用线程。我写了一个实现Runnable的类,它可以并发运行到另一个线程。主线程处理侦听串行端口,而第二个线程将处理将数据发送到同一端口。
public class MyNewThread implements Runnable {
    Thread t;

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

    public void run()  {
        // New Thread code here 
    }
第一个线程开始第二个像这样:
public class Main {
    public static void main(String[] args) throws Exception{
        new MyNewThread();
        // First thread code there
    }  
}
这有效,但我的编译器标记了一个警告说:在构造函数中启动一个新线程是危险的。为什么是这样? 这个问题的第二部分是:如果我在一个线程中运行一个循环(串口侦听线程),我在第二个线程中输入一个exit命令。如何获得第一个终止线程?谢谢。     
已邀请:
对于你的第一个问题:在传递
this
的构造函数中启动一个线程逃脱
this
。这意味着您实际上是在完全构造对象之前给出了对象的引用。线程将在构造函数完成之前启动。这可能导致各种奇怪的行为。 对于你的第二个问题:没有可接受的方法强制另一个线程在Java中停止,所以你将使用一个线程将检查的变量来知道它是否应该停止。另一个线程将其设置为指示第一个线程将停止。变量必须是volatile或所有访问同步以确保正确发布。这里有一些代码可能就像你想要的那样。
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
         }
    }
}
无论想要创建和启动线程,都可以:
MyNewThread thread = new MyNewThread();
thread.start();
无论什么想要阻止线程会做:
thread.stop();
    
让我们来看一个基本的例子:
class MyClass implements Runnable{
   int a = 0;
   String b = null;

   public MyClass(){
       new Thread(this).start();
       b = "Foo";
   }

   public void run(){
      a = b.length(); //can throw NullPointerException
   }
}
在这个例子中,MyClass.this被称为逃避构造函数。这意味着该对象可用于引用,但可能无法创建在构造函数中构建的所有字段。把这个带到另一个层次,如果b是
final
你会期望它可用但是没有确保。这被称为部分构造的对象,在java中完全合法。     
关于第二个问题, 您可以通过
isAlive
方法检查第二个线程是否已被终止,如果是,则使用
break
关键字关闭第一个线程的循环,如果没有必要,将终止
public class MyNewThread implements Runnable {
Thread t;

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

public void run()  {

   reading code ................
    // New Thread code here 
}
public class Main {
public static void main(String[] args) throws Exception{
   MyNewThread thread = new MyNewThread();

while(true)
{
    listening code ...................

    if(!thread.t.isAlive())
      break;
 }

}  
}
    
  这个问题的第二部分是:   如果我在一个循环中运行怎么办?   线程(串口侦听线程)   我在我的输入中输入一个退出命令   第二个线程。我怎么得到第一个   线程终止? 让它一直循环直到达到条件。例如:
public void run() {
    while ( !inputConsole.getCommand().equals("exit") ) {
        //Do Something
        Thread.sleep(1000); //put thread to sleep for 1 second
    }
}
    

要回复问题请先登录注册