我如何能够平等地从一个List共享一些数据?

时间:2017-04-10 16:10:43

标签: java multithreading

我搜索了其他主题的答案,但我真的不太了解。 我真正想要的是:假设我在ArrayList中有一些数据要处理和两个线程(或者可能是3?)。如何让这些线程平等地获取数据(并处理它)?

e.g。 :对于一个有10个元素和2个线程的arraylist,每个有5个元素,对于一个有10个元素和3个线程的arraylist,每个有3个元素,有4个元素。

额外的问题:我可以具体说一个特殊线程必须启动吗?

这是我运行以下代码所得到的:

  • 要在第一处理器中处理的数据:0 1 2 3 4 5 6 7 8 9
  • 要在第二处理器中处理的数据:
  • 可用数据:

(或随机的东西)

我真正想要的是: - 在第一处理器中处理的数据:0 2 4 6 8(或0 1 2 3 4) - 要在第二处理器中处理的数据:1 3 5 7 9(或5 6 7 8 9) - 可用数据:

公共类数据{

private List<Integer> dataIndex = new ArrayList<>();

Data() {
    for (int i = 0; i < 10; i++) {
        dataIndex.add(i);
    }
}

public synchronized Integer extractOneData(){
    return dataIndex.remove(0);

}

public List<Integer> getDataIndex() {
    return dataIndex;
}

public void printDataIndex() {
    System.out.println("Data available:");
    for (Integer i : dataIndex) {
        System.out.print(i + " ");
    }
}

公共类DataProcessor实现Runnable {

private Data data;
private String name;
private List<Integer> dataToProcess = new ArrayList<>();

DataProcessor(Data data,String name){
    this.data=data;
    this.name=name;
}

public void run(){
    while(!data.getDataIndex().isEmpty()) {
        dataToProcess.add(data.extractOneData());
    }
}

public void displaydataToProcess(){
    System.out.println("Data to be processed in " +name + ":");
    for(Integer i:dataToProcess){
        System.out.print(i+" ");
    }
    System.out.println();
}

}

public class Main {

public static void main(String[] args) {

    Data d = new Data();

    DataProcessor dp1 = new DataProcessor(d,"First processor");
    DataProcessor dp2 = new DataProcessor(d,"Second processor");

    Thread t1 = new Thread(dp1);
    Thread t2 = new Thread(dp2);

    t1.start();
    t2.start();

    dp1.displaydataToProcess();
    dp2.displaydataToProcess();

    d.printDataIndex();
}

1 个答案:

答案 0 :(得分:-2)

为了方便起见,我已将Data和DataProcessor作为内部类,但您可以在本地

上将它们分开
public class ForcedMultithreading {

    public static void main(String[] args) {

        ForcedMultithreading f = new ForcedMultithreading();

        Data d = f.new Data();

        int numberOfThreads = 2;

        int perThreadLoad = d.size() / numberOfThreads;

        DataProcessor dp1 = f.new DataProcessor(d, "First processor", perThreadLoad);
        DataProcessor dp2 = f.new DataProcessor(d, "Second processor", perThreadLoad);

        Thread t1 = new Thread(dp1);
        Thread t2 = new Thread(dp2);

        t1.start();
        t2.start();

        dp1.displaydataToProcess();
        dp2.displaydataToProcess();

        d.printDataIndex();
    }

    class Data {

        private List<Integer> dataIndex = new ArrayList<>();

        Data() {
            for (int i = 0; i < 10; i++) {
                dataIndex.add(i);
            }
        }

        public int size() {
            return dataIndex.size();
        }

        public synchronized Integer extractOneData() {
            return dataIndex.remove(0);

        }

        public List<Integer> getDataIndex() {
            return dataIndex;
        }

        public void printDataIndex() {
            System.out.println("Data available:");
            for (Integer i : dataIndex) {
                System.out.print(i + " ");
            }
        }

    }

    class DataProcessor implements Runnable {

        private Data data;
        private String name;
        private int perThreadLoad;
        private List<Integer> dataToProcess = new ArrayList<>();

        DataProcessor(Data data, String name, int perThreadLoad) {
            this.data = data;
            this.name = name;
            this.perThreadLoad = perThreadLoad;
        }

        @Override
        public void run() {
            while (perThreadLoad > 0) {
                dataToProcess.add(data.extractOneData());
                this.perThreadLoad--;
            }
        }

        public void displaydataToProcess() {
            System.out.println("Data to be processed in " + name + ":");
            for (Integer i : dataToProcess) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }
}

同样在上面的代码中,大小,即10除以2,即两个线程,但你可以详细计算出数字&#39; size&#39;不能被&#39; numberOfThreads&#39;完全整除例如10/3