简体   繁体   English

Java-线程不想启动

[英]Java - Thread doesn't want to start

I have this console application, but for some reason the thread's run() method doesn't want to start. 我有这个控制台应用程序,但是由于某种原因,线程的run()方法不想启动。 The code seems long for the first time but I tried to organize it as much as I can. 该代码第一次看起来很长,但是我尽了最大的努力来组织它。

The result output: 结果输出:

eThread starting!! 

So it seems that CarManager.startFunctionalities() gets executed, but the line eThread.start() is not executed at all because the line "started" is not printed out. 因此,似乎执行了CarManager.startFunctionalities(),但根本没有执行eThread.start()行,因为没有打印出“ started”行。

Here is the sourcecode. 这是源代码。

The main class: 主班:

package rpicar.android;


public class AndroidEmulator{


    public static void main(String args[]) throws InterruptedException {
        CarManager cm = new CarManager ("localhost");
    }
}

CarManager: 汽车经理:

package rpicar.android;

import rpicar.common.Direction;
import rpicar.common.EnvironmentData;


public class CarManager {
    private  MotorManager mManager;
    private final String RPIADDRESS = "localhost";
    private Thread mThread; //motor
    private EnvironmentManager eManager;
    private Thread eThread;
    public CarManager(String rpiAddress) {
        //initialize MotorManager
        mManager = new MotorManager(RPIADDRESS);
        //Make a thread for the Motor commands
        mThread = new Thread(mManager);
        //Initialize EnvironmentManager
        eManager = new EnvironmentManager(RPIADDRESS);
        //Makea thread for collecting EnvironmentData
        eThread = new Thread (eThread);
        startFunctionalities();
    }

    public void move(Direction d){
        this.mManager.setDirection(d);
    }

    public EnvironmentData getCurrentEnvironmentData(){
        return this.eManager.getCurrentEnvironmentData();
    }

    private void startFunctionalities(){
        //Start MotorManager for sending movement commands when needed.
        //mThread.start();
        //Start EnvironmentManager to collect EnvironmentData
        System.out.println("eThread starting!! ");
        eThread.start();
    }
}

EnvironmentManager: EnvironmentManager:

package rpicar.android;

import rpicar.common.CarComponent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import rpicar.common.EnvironmentData;


public class EnvironmentManager extends CarComponent implements Runnable{
    private EnvironmentData currentEnvironmentData;

    public EnvironmentManager(String rpiAddress) {
        super(rpiAddress, 2176, true);
        this.currentEnvironmentData = new EnvironmentData();
    }

    public synchronized EnvironmentData getCurrentEnvironmentData() {
        return currentEnvironmentData;
    }

    public synchronized void setCurrentEnvironmentData(EnvironmentData currentEnvironmentData) {
        this.currentEnvironmentData = currentEnvironmentData;
    }

    @Override
    public void run() {
        System.out.println("eThread started!! ");
        super.connect();
        while(true){
            try {
                this.setCurrentEnvironmentData((EnvironmentData) super.in.readObject());
            } catch (IOException ex) {
                super.connect();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(EnvironmentManager.class.getName()).log(Level.SEVERE, null, ex);
            }    
        }

    }
}

When you create your instance of eThread , you accidentally pass the thread itself to the constructor (or according to the order of the operations, you pass null ). 在创建eThread实例时,您不小心将线程本身传递给了构造函数(或者根据操作顺序,传递了null )。

You should pass eManager to the constructor instead. 您应该将eManager传递给构造函数。

 eThread = new Thread (eThread); 

Would become 会成为

eThread = new Thread (eManager);

You can protect yourself in the future from this mistake by making the eThread a final field, so you cannot use it before you declare it. 通过将eThread为final字段,可以在将来保护自己免受此错误的eThread ,因此在声明它之前不能使用它。

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

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