有多个业务,业务间可并行,但业务内需要顺序执行,利用java如何实现

qq_27434143 2018-07-10 02:32:57
业务数是不确定的,也可将业务理解为人
...全文
278 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_40852944 2018-07-10
  • 打赏
  • 举报
回复

public class Test {
private static Integer total = 20;

public static void main(String[] args) {
System.out.println("目前总工作量"+total+"份,请输入工作人数:");
Scanner scanner = new Scanner(System.in);
Integer persons = scanner.nextInt();
int var0 = total/persons;
for(int i=0;i<persons;i++){
new Thread(new Woker(var0,"Thread-"+i)).start();
}
}
}

class Woker implements Runnable{
private Integer total;

private String wokerName;

public Woker(Integer total, String wokerName) {
this.total = total;
this.wokerName = wokerName;
}

@Override
public void run(){
for(int i = 0;i<total;i++){
System.out.println(wokerName+"正在开始任务"+i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(wokerName+"已经完成任务"+i);
}
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧