问题
I have to add the objects of a threadpool to an ArrayList
, in order to count the total amount of objects my threadpool has created. For that I have created an ArrayList
, and after I execute my threadpool I add the object to the ArrayList
. My problem is that the new threadpools overwrite the existing object in my Arraylist
and therefore the Arraylist
will always stay the same size, no matter how many times the code is run.
My ArrayList
:
protected ArrayList<Runnable> ComponentBuild = new ArrayList<>();
Where the threads are started:
public void tireComponent() {
Menu menu = new Menu();
ExecutorService executor = Executors.newFixedThreadPool(3);
// ThreadPoolExecutor executor = new ThreadPoolExecutor();
for (int i = 1; i <= 3; i++) {
Runnable tire = new Tire("" + i);
executor.execute(tire);
ComponentBuild.add(tire);
System.out.println(ComponentBuild);
if (ComponentBuild.size() == MAX_CAPACITY) {
sleep();
}
}
Tire class:
public class Tire implements Runnable, Component {
private String number;
public Tire(String Number) {
this.number = Number;
}
@Override
public void run() {
System.out.println("Tire number " + number + " has now begun creation");
processCommand();
System.out.println("Thread " + Thread.currentThread().getName() + " has now created number " + number + " tire");
}
private void processCommand() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return this.number;
}
}
Outcome in terminal:
You have the following options:
1) Create normal trie
2) Create winter tire
3) Create premium tire
1
[1]
Tire number 1 has now begun creation
[1, 2]
Tire number 2 has now begun creation
[1, 2, 3]
You have the following options:
1) Create normal trie
2) Create winter tire
3) Create premium tire
Tire number 3 has now begun creation
Thread pool-1-thread-2 has now created number 2 tire
Thread pool-1-thread-1 has now created number 1 tire
Thread pool-1-thread-3 has now created number 3 tire
1
[1]
Tire number 1 has now begun creation
[1, 2]
Tire number 2 has now begun creation
[1, 2, 3]
You have the following options:
1) Create normal trie
2) Create winter tire
3) Create premium tire
Tire number 3 has now begun creation
Thread pool-2-thread-1 has now created number 1 tire
Thread pool-2-thread-2 has now created number 2 tire
Thread pool-2-thread-3 has now created number 3 tire
I would like it so the output would be like this
Tire number 2 has now begun creation
[1, 2, 3, 4, 5, 6]
But the actual output is the second threadpool overwriting the existing objects in my array, so no matter how many times I run the code, the ArrayList
will always be:
[1, 2, 3]
Edit: As requested, my Component interface:
package Model;
public interface Component {
void run();
String toString();
}
回答1:
You are running your loop only three times 'for (int i = 1; i <= 3; i++)' so it will have only three. If you want more you need to increase the loop size.
来源:https://stackoverflow.com/questions/58186424/adding-thread-product-to-array