gpt4 book ai didi

Java多线程系统

转载 作者:行者123 更新时间:2023-12-02 04:16:57 25 4
gpt4 key购买 nike

Java 多线程系统,在单线程上执行执行器。与可运行程序相比,使用执行程序有什么优点?

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;


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 的两个实现(InternalLogExternalLog),您的 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 模块的配置方法中。

bind(LogManager.class)
.toProvider(LogManagerProvider.class);

关于Java多线程系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33204333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com