Partitioning and analyzing Java arrays using multithreading

I have to initialize a floating point [12000] 12000 times through the for loop Then I scan the array for values that exceed a specific threshold If the value exceeds the threshold, I manipulate the instance variable of an object

Example:

Random random = new Random();
float[] x = new float[12000];

for (int i = 0; i < x.length; i++) {
  x[i] = random.nextFloat();
}

for (int i = 0; i < x.length; i++) {
  if (x[i] >= 0.75) {
  \\ do something interesting
  }
}

Basically, I have to change the value of the array and execute 12000 times on a new array with a length of 12000 The "interesting" code simply looks up the index in another data structure and calls setter According to my system time, I need about 13 hours There are eight processors on my machine

How to use the multithreading function of Java? I'm looking for a thread solution for partition initialization and array scanning The use of thread source code will be appreciated

Solution

You can divide it into eight different threads

public class Worker implements Runnable {
    final private int minIndex; // first index,inclusive
    final private int maxIndex; // last index,exclusive
    final private float[] data;

    public Worker(int minIndex,int maxIndex,float[] data) {
        this.minIndex = minIndex;
        this.maxIndex = maxIndex;
        this.data = data;
    }

    public void run() {
        for(int i = minIndex; i < maxIndex; i++) {
            if(data[i] >= 0.75) {
                // do something interesting
            }
        }
    }
}


// *** Main Thread ***
float[] data = new float[12000];
int increment = data.length / 8;
for(int i = 0; i < 8; i++) {
    new Thread(new Worker(i * increment,(i + 1) * increment,data)).start();
}

This divides the array into eight different threads Alternatively, the alternative is:

public class Worker implements Runnable {
    final private BlockingQueue<Integer> queue;
    final private float[] data;

    public Worker(BlockingQueue<Integer> queue) {
        this.queue = queue;
        this.data = data;
    }

    public void run() {
        while(true) {
            int i = queue.take();
            float f = data[i];
            // do something interesting to f
        }
    }
}


// *** Main Thread ***
BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
float[] data = new float[12000];
for(int i = 0; i < 8; i++) {
    new Thread(new Worker(queue,data)).start();
}
for(int i = 0; i < data.length; i++) {
    if (data[i] >= 0.75) {
        queue.offer(i);
    }
}

This uses a thread to iterate through the array and find interesting numbers, and then uses eight worker threads to do something interesting with interesting numbers I prefer this method, because the first method may cause one worker thread to process a thousand interesting numbers, while the other worker thread only needs to process some interesting numbers; This approach ensures that each thread needs to process approximately the same number of interesting numbers

I omitted a lot of things, such as how to use executors and how to close your worker thread – here's a tutorial on that

To edit the code to get and run 12000 times on 8 threads, you will do the following:

public class Worker implements Runnable {
    private final int numberOfIterations;
    private final float[] x = new float[12000];

    public Worker(int numberOfIterations) {
        this.numberOfIterations = numberOfIterations;
    }

    public void run() {
        for(int i = 0; i < numberOfIterations; i++) {
            Random random = new Random();

            for (int i = 0; i < x.length; i++) {
                x[i] = random.nextFloat();
            }

            for (int i = 0; i < x.length; i++) {
                if (x[i] >= 0.75) {
                    \\ do something interesting
                }
            }
        }
    }
}


// *** Main Thread ***
Thread[] threads = new Thread[8];
for(int i = 0; i < 8; i++) {
    threads[i] = new Thread(new Worker(12000/8));
    threads[i].start();
}
for(int i = 0; i < 8; i++) {
    threads[i].join();
}

Each of the eight threads will run 1500 iterations of "initialize floating point array, iterate floating point array" code The join method then waits for the thread to complete Make sure that / / making some interesting code is thread safe - you say you are calling a setter, so make sure that multiple threads will not call the same setter, otherwise the setter will be synchronized, or you will use something like atomicinteger in the setter If you have any questions about it, please release the setter code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>