gpt4 book ai didi

Java:Threadpoolexecutor - 使用作业列表来提交作业?

转载 作者:行者123 更新时间:2023-11-29 04:14:55 25 4
gpt4 key购买 nike

下面的代码是一个CustomerjobManager。客户有姓名、地​​址和账户余额。工作是从一个客户到另一个客户的汇款。这是一个 Threadpoolexecutor 训练程序。以下版本有效,我一个一个地提交作业。

客户.java

public class customer {

private String name;
private String adress;
private Double accountBalance;

public customer(String name, String adress, Double accountBalance)
{
this.name = name;
this.adress = adress;
this.accountBalance = accountBalance;
}

public String getName() { return name; }

public String getAdress()
{
return adress;
}

public Double getAccountBalance(){return accountBalance;}

public void setAccountBalance(double accountBalance){this.accountBalance=accountBalance;}

@Override
public String toString(){

return "[" + name+"; " +adress+"; "+accountBalance+"]";
}
}

customerOrganizer.java

import java.util.ArrayList;
import java.util.List;

public class customerOrganizer {

private static final customerOrganizer myJobOrganizer = new customerOrganizer();

public static customerOrganizer getJobOrganizer(){
return myJobOrganizer;
}

private List<customer> customerList = new ArrayList<customer>();

public void add_customer(customer kunde)
{
this.customerList.add(kunde);
}

public Iterable<customer> all_customers()
{
return this.customerList;
}

public static customerOrganizer getInstance()
{
return myJobOrganizer;
}

}

工作.java

public class job implements Runnable {
private customer kunde1;
private customer kunde2;
private Double transfer;

public job(customer kunde1, customer kunde2, Double transfer) {
this.kunde1 = kunde1;
this.kunde2 = kunde2;
this.transfer = transfer;
}

@Override
public String toString(){

return "[" + kunde1+"; " +kunde2+"; "+transfer+"]";
}

public void run() {

System.out.println("Starting transfer");

Double geber = this.kunde1.getAccountBalance();
Double nehmer = this.kunde2.getAccountBalance();

Double geberNeu = geber - this.transfer;
this.kunde1.setAccountBalance(geberNeu);

Double nehmerNeu = nehmer + this.transfer;
this.kunde2.setAccountBalance(nehmerNeu);
System.out.println("Transfer done");
}
}

jobOrganizer.java

public class jobOrganizer {

private static final jobOrganizer myJobOrganizer = new jobOrganizer();

public static jobOrganizer getMyJobOrganizer() {
return myJobOrganizer;
}

private List<job> jobList = new ArrayList<job>();

public int getAmount(){ return jobList.size();}

public void add_job(job newJob) {
this.jobList.add(newJob);
}

public Iterable<job> all_jobs() {
return this.jobList;
}

public static jobOrganizer getInstance() {
return myJobOrganizer;
}

主.java

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {

public static void main(String[] args) {
customerOrganizer myCustomerOrganizer = new customerOrganizer();
jobOrganizer myJobOrganizer= new jobOrganizer();

customer mueller = new customer("Tim Mueller", "Strasse 1", 1077.00);
customer john = new customer("John Doe", "Strasse 2",503.00);
customer meier = new customer("John Meier", "Strasse 3", 8500.50);
customer wurst = new customer("Hans Wurst", "Strasse 4", 1000.00);

myCustomerOrganizer.add_customer(mueller);
myCustomerOrganizer.add_customer(john);
myCustomerOrganizer.add_customer(meier);
myCustomerOrganizer.add_customer(wurst);

job transfer1= new job(meier,wurst,500.50);
job transfer2= new job(mueller,john,77.00);

myJobOrganizer.add_job(transfer1);
myJobOrganizer.add_job(transfer2);

// this works:
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.submit(transfer1);
executor.submit(transfer2);
executor.shutdown();


}}

所以,我确实有一个 jobList,而且我有想法,我应该使用它。我不想一个接一个地提交作业,而是根据作业列表提交它们。一开始我想到了这样的事情:

 int threads = myJobOrganizer.getAmount();
ExecutorService executor = Executors.newFixedThreadPool(threads);
for (int i = 0; i <threads+1; i++){
//submit jobs? execute?
}

此外,myJobOrganizer 需要实现 Runnable 吗?我还看到解决方案是这样的:

for(condition){

executor.execute(new Runnable() {

@Override
public void run() {
submit the jobs?
}
}}

但是我真的不知道,该怎么做。基本上,我缺少如何以正确的方式从我的 jobList 中提取作业,以便我可以将它们提交给执行程序服务>。<

关于线程安全的更新

因此,我点击了 Rab 提供的链接,我使用了 CompletionService。 Main.java 的最后一部分现在看起来像这样:

int threads = myJobOrganizer.getAmount();

System.out.println(myCustomerOrganizer.all_customers().toString());
// executor service
ExecutorService executor = Executors.newFixedThreadPool(threads);
// completion service is applied on executor
CompletionService service = new ExecutorCompletionService(executor);

for(Callable<Job> myJob : myJobOrganizer.all_jobs()){
service.submit(myJob);
}
executor.shutdown();
// pause the main for control printout -> not nice yet, I am working on
// joining threads
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// control print
System.out.println(myCustomerOrganizer.all_customers().toString());

请注意,此编辑旨在完成条目,但仍然是错误的(很遗憾)。提供的答案链接到原始问题,不涉及线程安全

感谢您的时间和努力!

最佳答案

ExecutorService处理如何在 worker 之间分配任务。您所要做的就是将工作一项一项地传递。

for (job jobObj : myJobOrganizer.all_jobs()) 
executor.submit(jobObj);

注意 sumbit返回 Future<?> ,用于跟踪您的任务是否已完成,或者它们是否出错(以及任务结果,但 runnable 没有结果)。如果你关心这些东西,你会想把它们收集在某种容器中,比如 List .


如果您更改 job进入 Callable<Void> ,提交会容易得多。 Callable是 Runnable 的某种扩展,允许任务在完成时产生结果。由于您的转账没有结果,使用java.lang.Void作为通用参数的填充类型就可以了。

现在,只需执行 executor.invokeAll(myJobOrganizer.all_jobs())就足够了。这将节省一些上下文切换,加快速度。 (其实很重要,因为你的任务都很小)


顺便说一句,您应该知道并发访问需要适当的同步,而您没有。如果不同的工作涉及同一个帐户,您的帐户最终可能会处于错误状态。我们还通常使用 LargeCamelCase 命名类,使用 smallCamelCase 命名方法。

关于Java:Threadpoolexecutor - 使用作业列表来提交作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53020725/

25 4 0
文章推荐: java - 如何在流 API 中使用后向范围?
文章推荐: php - 更新多行 Mysql PHP 需要很长时间并且崩溃
文章推荐: MySQL 8 不会在日志文件中的 "Data dictionary upgrading"之后启动
文章推荐: java - `return || ` 如何成为返回对象的方法的有效返回语句?