Adding thread product to array

亡梦爱人 提交于 2021-01-29 07:55:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!