简体   繁体   English

实现不带线程的Runnable的Java类

[英]Java Class that implements Runnable without thread

I have mode a class that implements the Runnable interface. 我有一个实现Runnable接口的类。

Now I want to start that class either multi-threaded, or non-multi-thraded (sequential) in my program according to a boolean. 现在,我想根据布尔值在我的程序中启动多线程或非多线程(顺序)类。

How would I go about doing that? 我将如何去做? This is my current code: 这是我当前的代码:

Constructor<?> constructor = processorClass.getConstructor(constructorParameterTypes);
Processor<T> process = (Processor<T>)constructor.newInstance(constructorParameters);
RunnableProcessor<T> runnableProcessor = new RunnableProcessor<>(process, object);
if (multiThreaded) {
    new Thread(runnableProcessor).start();
}
else {
    //what to do here?
}

So to clarify: I want to call the run() method from runnableProcessor , without creating a thread. 因此要澄清一下:我想从runnableProcessor调用run()方法,而不创建线程。 However I think that calling run() directly is deprecated, so looking for better solutions there. 但是我认为不建议直接调用run() ,因此在那寻找更好的解决方案。

Regards. 问候。

Simply call runnableProcessor.run(); 只需调用runnableProcessor.run(); , which is not deprecated (and as Runnable only has one method, run , it can't be deprecated without the whole class becoming deprecated too). ,它不会被弃用(并且由于Runnable仅具有一个方法run ,因此在不弃用整个类的情况下也无法弃用)。

Calling run is not deprecated and should be called for specific instances where sequential processing is required. 调用run不被弃用,对于需要顺序处理的特定实例,应调用它。

Alternatively you can have all your logic in a separate method, and call that method instead of calling run . 另外,您可以将所有逻辑放在单独的方法中,然后调用该方法而不是调用run run too will call this method inside it: run将在其中调用此方法:

run(){
doOp();
}

public doOp(){
//all your code here
}

Caller can call doOp instead of run but that effectively makes no difference! 调用者可以调用doOp而不是run但这实际上没有任何区别!

According to the documentation, run() is not deprecated, it's just a common mistake that people make when they're starting a new Thread , because where Runnable types are involved, people usually want to start a new Thread . 根据文档,不建议弃用run() ,这只是人们在启动新Thread时犯的一个常见错误,因为涉及Runnable类型时,人们通常希望启动新Thread

As you can see from the documentation , there is no deprecated tag there! 文档中可以看到,那里没有不推荐使用的标签!

Here is the simplest approach: 这是最简单的方法:

  1. If you want to run the class in a separate thread, call start method 如果要在单独的线程中运行类,请调用start方法
  2. If you don't want to run the class in a separate thread then call run method. 如果您不想在单独的线程中运行该类,请调用run方法。 Calling run will make the execution sequential in the same thread. 调用run将使执行在同一线程中顺序执行。

You can just call the run() method of the RunnableProcessor . 您可以只调用RunnableProcessorrun()方法。 As said before, it's not deprecated at all. 如前所述,它完全不被弃用。

Or you can call a new Thread , just like you do in the multithreaded situation, start it, and call thread.join() so that the main thread will wait for the newly started thread to finish. 或者,您可以像在多线程情况下那样调用新的Thread ,启动它,然后调用thread.join()以便主线程将等待新启动的线程完成。

Tis can be done in two approaches, Tis可以通过两种方法完成,

1.call the run() of RunnableProcess directly. 1.直接调用RunnableProcess的run()。

2.Move the logic to separate separate method and call that method in the non multithreaded scenario. 2.移动逻辑以分离单独的方法,然后在非多线程方案中调用该方法。 that method must also be invoked inside the run(). 该方法还必须在run()中调用。

run(){
    do();
}

do(){
    //your code here
}



if(multithreaded)
    new Thread(runnableProcess).start();
else
    runnableProcess.do();

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

相关问题 Java 多线程无线程 class 或可运行接口 - Java multithreading without thread class or runnable interface Java中的“实现可运行”与“扩展线程” - "implements Runnable" vs "extends Thread" in Java java匿名内部类实现Runnable和共享外部类对象是否是线程安全的? - Is it thread safe that java anonymous inner-class implements Runnable and shared outer-class object? 在实现可运行Java的类中放置延迟 - put a delay in a class that implements runnable java 调用创建其线程对象后实现runnable的Java类方法 - Call the method of Java class which implements runnable after creating its thread object 实现Runnable的类中的线程字段,它实例化所述类 - Thread field inside a class that implements Runnable, which instantiates said class 在Blackberry中从主线程调用扩展UiApplication并实现Runnable的类 - invoke class that extends UiApplication and implements Runnable from main thread in Blackberry 接口类实现Runnable - interface class implements Runnable Java 6线程-具有“可运行的实现”和“扩展线程”的不同行为 - Java 6 Threads - different behaviour with “implements Runnable” and “extends Thread” 实现 Runnable 的 class 被认为是 ExecutorServices 的“可运行任务”? - A class that implements Runnable is considered to be a 'runnable task' to ExecutorServices?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM