我是一名努力学习Java编程的学生。我遇到了Java中的线程池,但我对以下线程池的工作原理感到困惑。我给了它5个工作线程。我的问题是,它是如何只有一个工作线程来完成我的工作?因为在我的输出中它只显示它只有一个执行任务的工作线程。我可以使用哪个公式来了解放入固定线程池的工作线程数?另外请明确告诉我工作线程的含义是什么。我无法解释是否有人问我。
public class Executement implements Runnable{
Executement ec;
private int taskId;
public int men(int z,int x){
int y = this.taskId*z;
int w = this.taskId+x;
return(y & w);
}
public void run(){
for(int i = 0; i < 50; i++){
int z = i*2;
int m = i;
System.out.println("Task ID :" + z + " performed by " + Thread.currentThread().getName());
System.out.println("taskid: " + m + " performed by " + Thread.currentThread().getName());
}
}
public static void main(String[] args) {
ExecutorService service=Executors.newFixedThreadPool(5);
service.submit(new Executement());
service.shutdown();
}
}
这是输出的一部分:
Task ID :0 performed by pool-1-thread-1
taskid: 0 performed by pool-1-thread-1
Task ID :2 performed by pool-1-thread-1
taskid: 1 performed by pool-1-thread-1
Task ID :4 performed by pool-1-thread-1
taskid: 2 performed by pool-1-thread-1
Task ID :6 performed by pool-1-thread-1
taskid: 3 performed by pool-1-thread-1
Task ID :8 performed by pool-1-thread-1
taskid: 4 performed by pool-1-thread-1
Task ID :10 performed by pool-1-thread-1
答案 0 :(得分:3)
你给了它5个线程和一个工作。 submit()
更多Runnable
个任务。
答案 1 :(得分:0)
这将使用多个线程。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Executement implements Runnable {
private int taskId;
public Executement(int taskId) {
this.taskId = taskId;
}
public void run() {
System.out.println("task " + taskId + " performed by "
+ Thread.currentThread().getName());
}
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(5);
for (int i = 0; i < 50; i++) {
service.submit(new Executement(i));
}
service.shutdown();
}
}
不同之处在于您必须开始多个工作。只是在一个作业内部迭代不会使它产生多个线程。
答案 2 :(得分:0)
将主要方法更改为
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
service.submit(new Executement());
}
service.submit(new Executement());
service.shutdown();
}
你只给了一个任务。给更多任务和线程池将启动更多线程。 查看部分输出
Task ID :0 performed by pool-1-thread-1
Task ID :0 performed by pool-1-thread-2
taskid: 0 performed by pool-1-thread-2
Task ID :2 performed by pool-1-thread-2
taskid: 1 performed by pool-1-thread-2
Task ID :4 performed by pool-1-thread-2
taskid: 2 performed by pool-1-thread-2
Task ID :6 performed by pool-1-thread-2
taskid: 3 performed by pool-1-thread-2
Task ID :8 performed by pool-1-thread-2
taskid: 0 performed by pool-1-thread-1
Task ID :0 performed by pool-1-thread-3
taskid: 0 performed by pool-1-thread-3
Task ID :2 performed by pool-1-thread-3
taskid: 1 performed by pool-1-thread-3
Task ID :4 performed by pool-1-thread-3
taskid: 2 performed by pool-1-thread-3
Task ID :6 performed by pool-1-thread-3
taskid: 3 performed by pool-1-thread-3
Task ID :8 performed by pool-1-thread-3
Task ID :0 performed by pool-1-thread-5
taskid: 0 performed by pool-1-thread-5
Task ID :2 performed by pool-1-thread-5
taskid: 1 performed by pool-1-thread-5
Task ID :4 performed by pool-1-thread-5