问题描述
我必须将线程池的对象添加到 ArrayList
,以便计算我的线程池创建的对象总数.为此,我创建了一个 ArrayList
,并在执行线程池后将对象添加到 ArrayList
.我的问题是新线程池覆盖了我的 Arraylist
中的现有对象,因此 Arraylist
将始终保持相同的大小,无论代码运行多少次.
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.
我的ArrayList
:
protected ArrayList<Runnable> ComponentBuild = new ArrayList<>();
线程开始的地方:
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();
}
}
轮胎等级:
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;
}
}
终端结果:
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]
但实际输出是第二个线程池覆盖了我数组中的现有对象,所以无论我运行多少次代码,ArrayList
将始终是:
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]
根据要求,我的组件界面:
As requested, my Component interface:
package Model;
public interface Component {
void run();
String toString();
}
推荐答案
你的循环只运行了 3 次 'for (int i = 1; i
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.
这篇关于将线程产品添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!