Pseudorandomity in concurrent programs - java

I'm writing a program that generates a lot of random numbers. I used to set a fixed seed at the beginning to keep results replicable. The problem that I am facing now is that in a naive implementation I lose the pseudorandomity due to concurrency.
My question is this: Is it possible to preserve pseudorandomity without having inter-thread communication and if so, how?
The following code should illustrate my problem. Each run, the program returns a different triple of sums. Obviously, the order of access will also differ most of the time.
public class PseudorandomConcurrency {
private static Random rng;
private static int numDraws = 2;
private static int numThread = 3;
private static int numRuns = 3;
private static int order;
public static class Generator implements Runnable{
private static synchronized void incOrder(){
order++;
}
private static synchronized int getOrder(){
return order;
}
#Override
public void run() {
int sum = 0;
ArrayList<Integer> order = new ArrayList<Integer>();
for (int j = 0; j < numDraws; j++) {
incOrder();
order.add(getOrder());
sum += rng.nextInt(10);
}
System.out.println(sum+" order: "+order.toString());
}
}
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < numRuns; i++) {
order=0;
System.out.println("#####");
rng = new Random(1);
ArrayList<Thread> threads= new ArrayList<Thread>();
for (int j = 0; j < numThread; j++) {
threads.add(new Thread(new Generator()));
}
for (Thread thread : threads) {
thread.start();
}
Thread.sleep(1000);
}
}
}

My question is this: Is it possible to preserve pseudorandomity without having inter-thread communication and if so, how?
Yes it is. Make each thread use a separate random number generator (with a different but deterministic seed).

Related

Synchronized getter and synchronized thread run method in java

The followiing code uses threads to calculate the max value in a subarry, and then calculates the max value out of the max values the threads returned. I have a bug that the main thread doesn't wait for the threads to finish when collecting the results.
Thread class:
public class MaxTask extends Thread {
private int[] arr;
private int max;
private int first, last;
public MaxTask(int[] arr, int first, int last) {
this.arr = arr;
this.first = first;
this.last = last;
}
public int getMax() {
return max;
}
public void run() {
max = arr[first];
for (int i = first + 1; i <= last; i++) {
if (arr[i] > max) max = arr[i];
}
}
}
Main:
public class MainMax {
public static void main(String[] args) throws Exception {
int size = 100;
int workers = 10;
int[] arr = new int[size];
int max = 0;
for (int i = 0; i < size; i++) {
arr[i] = (int)(Math.random() * 100);
if (max < arr[i]) max = arr[i];
}
System.out.println("max=" + max);
int gsize = (arr.length - 1) / workers;
MaxTask[] tasks = new MaxTask[workers];
int first = 0;
int last;
for (int i = 0; i < workers; i++) {
last = first + gsize;
tasks[i] = new MaxTask(arr, first, last);
tasks[i].start();
first = last + 1;
}
int maxmax = tasks[0].getMax();
int temp;
for (int i = 1; i < workers; i++) {
temp = tasks[i].getMax();
if (temp > maxmax) maxmax = temp;
}
System.out.println("maxmax=" + maxmax);
}
}
I am trying to solve the problem using synchronized. I managed to get it working when using synchronized on both run and getMax. But I really don't understand why this solves the problem.
First, you must understand that the main class is also running on a thread. That thread is seperate from the threads you created for the function and is thus running in parallel to them. By that logic, int maxmax = tasks[0].getMax(); is running asynchronously and possibly before the loop is finished.
One possible solution would be to lock that part of the code and force the execution to wait before executing that line. Only release the lock after everyone in the loop is done. Synchronizing access to the run method only defeats the purpose of running multiple threads since you're forcing the whole thing to be sequential.
It is also not recommended to create a thread for every single element, since there's a tradeoff between number of threads and how much you're speeding up execution.

Multiply three matrices using threads in java

I'm trying to multiply three matrices using threads. I use a number of threads for a matrix multiplication and I should use another number of threads for multiply the result of the two matrices with the last matrices, and I would like to start the threads for the second multiplication as soon as they start having data from the first multiplication result. I'm a little stuck.
What I've tried:
public class Threads implements Runnable{
private String action;
private int range;
public Threads(String action, int range) {
this.action = action;
this.range = range +1;
}
public synchronized void run() {
int k;
int counter = 0;
int m_size = Program.m1.length;
int k_size =Program.m1[0].length;
int n_size = Program.m2[0].length;
int h_size = Program.m3[0].length;
for (int i=0; i<m_size; i++)
for (int j=0; j<n_size; j++) {
counter++;
if (counter % range == 0) {
int val1 = 0;
for (k = 0; k < k_size; k++) {
Program.result[i][j] += Program.m1[i][k] * Program.m2[k][j];
// int val1 =0;
for (int h=0; h<h_size; h++){
val1 += Program.result[i][h] * Program.m3[h][j];
}
}
Program.result_matrix[i][j] = val1; //here run error
}
}
}
public class Program {
private final static Random rand = new Random();
public static int[][] m1 = new int[][]{};
public static int[][] m2 = new int[][]{};
public static int[][] m3 = new int[][]{};
public static int[][] result = new int[][]{};
public static int[][] result_matrix = new int[][]{};
public static void generate(String action, int number_of_threads) throws InterruptedException {
List<Thread> all_threads = new ArrayList<>();
for (int th = 0; th < number_of_threads; th++) {
Thread t = new Thread(new Threads(action, th));
all_threads.add(t);
t.start();
}
for (Thread t : all_threads) {
t.join();
}
}
public static void main(String[] args) throws Exception {
{ //here i m reading the matrices
generate(action, number_of_threads);
print_result_matrix();
public static void print_result_matrix() {
for (int i = 0; i < result_matrix.length; i++) {
for (int j = 0; j < result_matrix[0].length; j++)
System.out.print(result_matrix[i][j] + " ");
System.out.println();
}
}
When I run this program I got an error after setting the matrices rows and columns and gives me error at line //look at code.
Anyway some ideas?I know that my code doesn't have a logic in run() method at the end but I really don't know how to achieve this.
Any ideas?
Stacktrace:
Exception in thread "Thread-0" Exception in thread "Thread-1 java.lang.ArrayIndexOutOfBoundsException: 0
at Threads.run(Threads.java:43)
at java.lang.Thread.run(Thread.java:745) java.lang.ArrayIndexOutOfBoundsException: 0
at Threads.run(Threads.java:43)
at java.lang.Thread.run(Thread.java:745)
You need to initialize result matrices' sizes:
public static int[][] result = new int[m1.length][m2[0].length];
public static int[][] result_matrix = new int[result.length][m3[0].length];
Of course, this should be done after m1, m2 and m3 are filled with data.

using threads on an Atomic Integer Array

I am writing code to let 4 threads build a histogram.
I have an array in main:
int N = 10000;
Random r = new Random();
int[] a = new int[N];
for (int i = 0; i < a.length; i++)
{
a[i] = Math.abs(r.nextInt() % 100);
}
So basically what I want to do is cycle through this array and count how many times each number appears.
So I have written my thread class, and I used AtomicInteger which I thought would help solve the problem of multiple threads trying to access the same index simultaneously.
import java.util.concurrent.atomic.AtomicInteger;
public class UseThread implements Runnable
{
private static int[] array;
private static AtomicInteger[] count;
private static boolean[] check;
public UseThread(int[] array, AtomicInteger[] count)
{
this.array = array;
this.count = count;
this.check = new boolean[array.length];
}
public void run()
{
for (int i = 0; i < array.length; i++)
{
if (!getIndex(this.check[i]))
{
this.check[i] = true;
int number = array[i];
count[number].incrementAndGet();
}
}
}
public synchronized static boolean getIndex(boolean check2)
{
return check2;
}
However, this hasn't quite fixed my problem. Some of the threads are accessing the array at the same time, making the count array, hold a larger value than the length of the array array.
I think the problem is in the boolean array of checking. I have a feeling that multiple threads access the same boolean array index at the same time.
I figure it is probably a simple fix, but I am just not seeing it.
Any suggestions??
I have tried the AtomicBoolean array, but it has not helped. Below is the same class but with the AtomicBoolean array implemented.
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
public class Assign7Q3 implements Runnable
{
private static int[] array;
private static AtomicInteger[] count;
private static AtomicBoolean[] check;
public Assign7Q3(int[] array, AtomicInteger[] count)
{
this.array = array;
this.count = count;
this.check = new AtomicBoolean[array.length];
for(int i = 0; i < check.length; i ++)
check[i] = new AtomicBoolean(false);
}
public void run()
{
for (int i = 0; i < array.length; i++)
{
//System.out.println(this.check[i].get());
if (!getIndex(this.check[i]))
{
this.check[i].set(true);
int number = array[i];
count[number].incrementAndGet();
}
}
}
public synchronized static boolean getIndex(AtomicBoolean check2)
{
return check2.get();
}
You need to use compareAndSet for your if statement to be mutally exclusive:
if (this.check[i].compareAndSet(false, true))
{
int number = array[i];
count[number].incrementAndGet();
}
This both checks and sets the value atomically.
Without compareAndSet, there is a possibility that two threads can check the value and enter the if block at the same time before one has a chance to call set(true).

How to simulate in Java a stale value read by a thread?

I would like to simulate a situation which is mentioned in books about concurrency - that without a proper synchronization one thread can see a stale value of a variable that has been already modified by a different thread. This could happen because for example a CPU cache.
To do this I have written the following program. The idea is that there are 4 threads that initialize a different part of a shared array. The 5th thread (main, parent thread) waits until all 4 previous threads are done, iterates over the shared array and adds its values (always 1 or if I'm lucky null, which would mean a stale value)
package p1;
class ArrFill implements Runnable {
int l, r;
Integer[] arr;
ArrFill(int l, int r, Integer[] arr) {
this.l = l;
this.r = r;
this.arr = arr;
}
#Override
public void run() {
for(int i = l; i < r; i++)
arr[i] = new Integer(1);
}
}
public class Main {
final static int MAX = 10000000;
final static int tnum = 4;
public static void main(String[] args) throws InterruptedException {
int cores = Runtime.getRuntime().availableProcessors();
System.out.println(cores);
Integer[] arr = new Integer[MAX];
Thread[] t = new Thread[tnum];
if(MAX % tnum != 0)
throw new IllegalStateException();
int step = MAX / tnum;
int l = 0, r = 0;
for(int i = 0; i < tnum; i++) {
l = r;
r += step;
t[i] = new Thread(new ArrFill(l, r, arr));
t[i].start();
}
for(int i = 0; i < tnum; i++)
t[i].join();
int res = 0;
for(int i = 0; i < MAX; i++)
if(arr[i] != null)
res += arr[i];
System.out.println(res == MAX);
}
}
I have run this program many times although I never seen a stale value (null). I have 2 cores. Do you have any idea how this program could be improved to actually present the cached value phenomena? Or maybe you have a completly different approach?
Thanks!

java concurrency problem

I want to make a simple math operations on a vector(array) using two cores of my CPU. The program doesn't work correctly. Please explain me how to solve my problem.
public class MyRunnable implements Runnable {
private int startIndex;
private int endIndex;
private float[] tab;
public MyRunnable(int startIndex, int endIndex, float[] tab)
{
this.startIndex = startIndex;
this.endIndex = endIndex;
this.tab = tab;
}
#Override
public void run()
{
System.out.println(Thread.currentThread());
for(int i = startIndex; i < endIndex; i++)
{
tab[i] = i * 2;
}
System.out.println("Finished");
}
}
public class Test {
public static void main(String[] args) {
int size = 10;
int n_threads = 2;
float tab[] = new float[size];
for(int i = 0; i < size; i++)
{
tab[i] = i;
}
System.out.println(Thread.currentThread());
for(int i = 0; i < size; i++)
{
System.out.println(tab[i]);
}
Runnable r1 = new MyRunnable(0, size / n_threads, tab );
Runnable r2 = new MyRunnable(size / n_threads, size, tab );
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
for(int i = 0; i < size; i++)
{
System.out.println(tab[i]);
}
}
It seems like you don't wait for the threads to finish. Use the join method and add
t1.join();
t2.join();
just before the output loop.
As pointed out by others, you are not waiting for your threads to finish execution. You should follow the advice of #Howard and #JK and that will fix your basic issue. If you decide to do more with threads and parallel processing though I would highly advice looking into the java.util.concurrent packages - they have many useful classes that will make your life much easier.
I took the liberty of recoding your example using Callable and ExecutorService. Please see the sample code below:
public static void main(String[] args) {
int size = 10;
int n_threads = 2;
float tab[] = new float[size];
for (int i = 0; i < size; i++) {
tab[i] = i;
}
System.out.println(Thread.currentThread());
for (int i = 0; i < size; i++) {
System.out.println(tab[i]);
}
// Determine batch size, based off of number of available
// threads.
int batchSize = (int) Math.ceil((double) size / n_threads);
System.out.println("Size: " + size + " Num threads: " + n_threads
+ " Batch Size: " + batchSize);
// Create list of tasks to run
List<Callable<Object>> tasks = new ArrayList<Callable<Object>>(
n_threads);
for (int i = 0; i < n_threads; i++) {
tasks.add(Executors.callable(new MyRunnable(i * batchSize,
((i + 1) * batchSize) - 1, tab)));
}
// Create an executor service to handle processing tasks
ExecutorService execService = Executors.newFixedThreadPool(n_threads);
try {
execService.invokeAll(tasks);
} catch (InterruptedException ie) {
ie.printStackTrace();
} finally {
execService.shutdown();
}
for (int i = 0; i < size; i++) {
System.out.println(tab[i]);
}
}
And made one slight change in your MyRunnable class, which was skipping processing on the last index:
#Override
public void run() {
System.out.println(Thread.currentThread());
for (int i = startIndex; i <= endIndex; i++) {
tab[i] = i * 2;
}
System.out.println("Finished");
}
Works great, you can test for yourself. Theres many more classes in java.util.concurrent that can do similar functionality, feel free to explore.
Good luck!
You can wait for the threads to finish execution by inserting calls to Thread.join():
t1.join();
t2.join();
after your x.start() function call to pause until the threads have completed. Otherwise you cannnot know if they are finished executing or not.
You should also consider synchronizing your tab[] accesses within the separate threads with a mutex/semaphore or similar mechanism, and not necessarily perform calculations directly on the passed in array reference, since this can limit the amount of concurrency (if present).

Categories

Resources