I'm attempting to use Executors instead of synchronized methods to manage threads for me. But the submit
method takes a Runnable task
and not really a Runnable target
, which is what I'm after, since I'm starting a single thread where the target class is going to be instantiated and ran, like so:
thread = new Thread(this, "thread-process");
thread.start();
我试图对执行者做一些等效的事情:
this.exec = Executors.newSingleThreadExecutor();
this.exec.submit(this);
The thread seems to be starting and running fine as far as I can tell, but I'm not really sure if these are equivalents? Is this the way I should be dealing with runnable target threads when using Executors
?
Yes, given a
Runnable
(an object with arun
method) such as this:…然后这样做:
…实际上与执行此操作相同:
You might being getting hung up on the variable naming. Variable naming is irrelevant here. The
Thread
constructor Javadoc usestarget
, as seen copy-pasted in my comment in myrun
method. Yet I chose to go with a variable namedrunnable
. You might choose a name likeexportQuarterlySalesDataRunnable
orpinkElephant
.