Java – shuffling array in multithreading

I have an array of size n I want to shuffle its elements in 2 threads (or more) Each thread should use its own array part

Suppose that the first thread moves the element from 0 to K, and the second thread moves the element from K to n (where 0 < = ">

//try-catch stuff is ommited
static void shuffle(int[] array) {
   Thread t1 = new ShufflingThread(array,array.length / 2);
   Thread t2 = new ShufflingThread(array,array.length / 2,array.length);
   t1.start();
   t2.start();
   t1.join();
   t2.join();
}

public static void main(String[] args) {
   int array = generateBigSortedArray();
   shuffle(array);
}

Is there any guarantee from the JVM that I will see the array changes in the main method after such a reorganization?

How should I implement the shufflingthread (or how should I run it, possibly in a synchronization block or anything else) to obtain such a guarantee?

Solution

Thread. Start() and thread Join () is enough to provide you with the prior relationship between array initialization, thread switching and readback in the main method

The action that caused the occurrence is documented here

As mentioned elsewhere, forkjoin is well suited to this divide and conquer algorithm, which can get you out of many bookkeeping you need to implement

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
分享
二维码
< <上一篇
下一篇>>