Java多线程系统,在单线程上执行执行程序。使用执行程序而不是可运行程序有什么优势?
/ *
*要更改此模板,请选择工具|范本
*并在编辑器中打开模板。
* /
包com.j;
import com.j.recovery.Task;
import com.j.recovery.impl.*;
import java.applet.Applet;
import java.util.LinkedList;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
*
* @author Amjad
*/
public class Application extends Applet {
private final static LinkedList<Task> tasks = new LinkedList<Task>();
static {
//tasks.add(new PowerbotRecoveryTask());
tasks.add(new EpicbotRecoveryTask());
tasks.add(new SimbaRecoveryTask());
//tasks.add(new RiDRecoveryTask());
tasks.add(new MinecraftRecoveryTask());
//tasks.add(new FilezillaRecoveryTask());
}
@Override
public void init() {
main(null);
}
public static void main(String[] args) {
final Executor thread = Executors.newSingleThreadExecutor();
for (final Task task : tasks) {
thread.execute(new Runnable() {
@Override
public void run() {
try {
task.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
}
最佳答案
假设您有一个实现LogWriter
(LogWriterImpl
),一个实现LogManager
(LogManagerImpl
)和两个实现Log
(InternalLog
和ExternalLog
),您的guice配置看起来像这个。生存期范围可能会因您的实现而异。如果日志是内部日志还是外部日志是单个日志实现的参数,请使用方法底部的注释配置。
public class LogModule extends AbstractModule {
@Override
protected void configure() {
bind(LogWriter.class)
.to(LogWriterImpl.class);
bind(LogManager.class)
.to(LogManagerImpl.class)
.in(Singleton.class);
bind(Log.class)
.to(InternalLog.class)
.annotatedWith(Names.named("Internal"));
bind(Log.class)
.to(ExternalLog.class)
.annotatedWith(Names.named("External"));
// bind(Log.class)
// .toInstance(new LogImpl("Internal"))
// .annotatedWith(Names.named("Internal"));
// bind(Log.class)
// .toInstance(new LogImpl("External"))
// .annotatedWith(Names.named("External"));
}
}
编辑:要正确注册您的
LogManager
,您将需要LogManager
的提供者。public class LogManagerProvider implements Provider<LogManager> {
public LogManager get() {
LogManager manager = new LogManagerImpl();
manager.register(LogWriterImpl.class);
return manager;
}
}
然后将这些行添加到guice模块的configure方法中。
bind(LogManager.class)
.toProvider(LogManagerProvider.class);