Reputation: 3541
I am a student trying hard to excel in Java programming. I came across thread pools in Java but I am really confused on how the following thread pool works. I have given it 5 work threads. My question is how come it's only one worker thread that does my work? Because in my output it only shows that it's only one worker thread that has executed the task. Which formula can I use to know the number of worker-threads to put in the fixed thread-pool? Also please explain to me clearly what is the meaning of a worker thread. I'm not able of explaining if somebody were to ask me.
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();
}
}
here is part the output:
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
Upvotes: 0
Views: 586
Reputation: 1525
This will use multiple threads.
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();
}
}
The difference is that you must start multiple jobs. Just iterating inside one job will not make it spawn multiple threads.
Upvotes: 0
Reputation: 1869
Change your main method to
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();
}
You only gave one task. Give more tasks and thread pool will start more threads. See part of the output your self
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
Upvotes: 0
Reputation: 113382
You gave it 5 threads, and one job. submit()
more Runnable
tasks.
Upvotes: 3