[英]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: 这是最简单的方法:
start
method start
方法 run
method. run
方法。 Calling run will make the execution sequential in the same thread. You can just call the run()
method of the RunnableProcessor
. 您可以只调用
RunnableProcessor
的run()
方法。 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.