组合两个可运行对象 [英] Combining two Runnable objects
问题描述
例如,假设我有一个名为RunnableA的Runnable,它可以执行某些操作.我还有一个名为RunnableB的Runnable,它可以执行其他操作.有什么办法可以将这两个Runnable组合在一起,以便它们在同一线程中运行?
Say for example that I have a Runnable called RunnableA that does something. I also have a Runnable called RunnableB that does something else. Is there a way that I can combine these two Runnables someway so that they will run in the same thread?
问题的第二部分是如果可以的话,我可以指定它们的运行顺序吗?
The second part of the question is if this is possible, can I then specify the order that they will run in?
编辑!:之所以要这样做,是因为我需要在EDT上运行代码,而其他一些代码则需要在另一个线程上运行.请看下面的代码.
EDIT!: The reason why I wanted to do this was because I need to run code on the EDT but some of the other code needs to be run on another thread. Please take a look at the code below.
类似这样的东西
public final class CompoundRunnable implements Runnable
{
private final Iterable runnables;
public CompoundRunnable(Iterable runnables)
{
// From Guava. Easy enough to do by hand if necessary
this.runnables = Lists.newArrayList(runnables);
}
public CompoundRunnable(Runnable... runnables)
{
this(Arrays.asList(runnables));
}
@Override
public void run()
{
for (Runnable runnable : runnables)
{
runnable.run();
}
}
}
public void setStatusAndProgress(final String status,Runnable runnable)
{
Runnable startUpRunner = new Runnable()
{
public void run()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
setStatus(status);
selfReference.getProgressBar().setIndeterminate(true);
}
});
}
};
Runnable cleanUpRunner = new Runnable()
{
public void run()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
setStatus("");
getProgressBar().setIndeterminate(false);
}
});
}
};
Runnable theRunner = new CompoundRunnable(startUpRunner,runnable,cleanUpRunner);
new Thread(theRunner).start();
}
很抱歉,如果解释不正确,请在需要澄清的地方发表评论.
Sorry if this isnt explained well, post comments if you need clarification.
谢谢!
推荐答案
当然,您可以创建一个Runnable
,它只运行一个可运行的,然后运行另一个:
Well you can certainly create a Runnable
which just runs one runnable then the other:
public final class CompoundRunnable implements Runnable
{
private final Runnable first;
private final Runnable second;
public CompoundRunnable(Runnable first, Runnable second)
{
this.first = first;
this.second = second;
}
@Override
public void run()
{
first.run();
second.run();
}
}
更一般而言,您可以将其取为Iterable<Runnable>
,复制所有Runnable
引用,然后按顺序运行它们.例如:
More generally, you could make it take an Iterable<Runnable>
, copy all the Runnable
references, and then run them in order. For example:
public final class CompoundRunnable implements Runnable
{
private final Iterable<Runnable> runnables;
public CompoundRunnable(Iterable<Runnable> runnables)
{
// From Guava. Easy enough to do by hand if necessary
this.runnables = Lists.newArrayList(runnables);
}
public CompoundRunnable(Runnable... runnables)
{
this(Arrays.asList(runnables));
}
@Override
public void run()
{
for (Runnable runnable : runnables)
{
runnable.run();
}
}
}
这篇关于组合两个可运行对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!