mardi 28 août 2018

Java. Thread finished instead of infinity execution

I have jar from developer. This jar contains class some.class.Executor, that can be started and works, until i kill the process.

There is two ways to run this class.

  1. From main() class (as entry point).
  2. From thread.

First option is simple:

public static void main(String[] args) {
    some.class.Executor.main(args);
}

And it works fine. But when i try run this from my thread like:

public class CustomExecutor extends AbstractExecutor {

public CustomExecutor(String scenario) {
    this.args[0] = scenario;
}

@Override
public String generateName() {
    return CustomExecutor.class.getSimpleName() + "-" + UUID.randomUUID().toString();
}

public void run() {
    some.class.Executor.main(getArgs());
}

Abstract executor is just:

public abstract class AbstractExecutor extends Thread {

protected String[] args;

AbstractExecutor() {
    this.args = new String[1];
    setName(generateName());
    setDaemon(false);
}

public abstract String generateName();

public void register() {
    WorkerStorage.getInstance().add(getName(), this);
}

public void launch() {
    register();
    WorkerStorage.getInstance().getThreadMap().get(getName()).start();
}

So basically i run same method, but from my thread. And it dies. Change thread state to TERMINATED.

Is there big difference between two run options? Why main() method have different behavior?

Aucun commentaire:

Enregistrer un commentaire