java concurrency problem - java

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).

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.

Java array split and matrix multiplication

Can someone help me with this, please? I'm trying to do a matrix multiplication, using threads. This is what I have so far:
//updated
public class Multiplication {
public static final int NUM_OF_THREADS = 8;
public static final int MATRIX_SIZE = 1000;
public static void main(String args[]) throws InterruptedException {
long startTime = System.currentTimeMillis();
int MatrixA[][] = matrixGenerator();
int MatrixB[][] = matrixGenerator();
int m1rows = MatrixA.length;
int m1cols = MatrixA[0].length;
int m2cols = MatrixB[0].length;
int MatrixC[][] = new int[m1rows][m2cols];
ExecutorService pool = Executors.newFixedThreadPool(NUM_OF_THREADS);
for (int row1 = 0; row1 < m1rows; row1++) {
for (int col1 = 0; col1 < m1cols; col1++) {
pool.submit(new MultiplicationThreading(row1, col1, MatrixA, MatrixB, MatrixC));
}
}
pool.shutdown();
pool.awaitTermination(1, TimeUnit.DAYS);
long endTime = System.currentTimeMillis();
System.out.println("Calculated in "
+ (endTime - startTime) + " milliseconds");
}
public static int[][] matrixGenerator() {
int matrix[][] = new int[MATRIX_SIZE][MATRIX_SIZE];
Random r = new Random();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = r.nextInt(10000);
}
}
return matrix;
}
}
//I have updated the code
I get better timings now. When using 2 threads I get 1.5k milliseconds and when I use 8 threads 1.3k milliseconds
You initialize the thrd array with NUM_THREADS == 9 elements. If m1rows*m1cols exceeds that value, you will get this problem, since you attempt to create more than 9 threads and assign them to elements of the array. (You are attempting to create 50 threads).
Two solutions:
Initialize thrd = new Thread[m1rows*m1cols]
Use a List<Thread>.
Note that you won't execute the threads in parallel, because you are calling Thread.join() immediately after calling Thread.start(). This just blocks the current thread until thrd[threadcount] finishes.
Move the Thread.join() calls into a separate loop, so the threads are all started before you call join on any of them.
for (row = 0; row < m1rows; row++) {
for (col = 0; col < m1cols; col++) {
// creating thread for multiplications
thrd[threadcount] = new Thread(new MultiplicationThreading(row, col, MatrixA, MatrixB, MatrixC));
thrd[threadcount].start(); //thread start
threadcount++;
}
}
for (Thread thread : thrd) {
thread.join();
}

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 Multithreading - a basic parallel example of Sieve of Eratosthenes

I would like to create a simple parallel Sieve of Erastosthenes Java program, that would be at least a bit more effective then a serial version I've posted below.
public void runEratosthenesSieve(int upperBound) {
int upperBoundSquareRoot = (int) Math.sqrt(upperBound);
boolean[] isComposite = new boolean[upperBound + 1];
for (int m = 2; m <= upperBoundSquareRoot; m++) {
if (!isComposite[m]) {
System.out.print(m + " ");
int threads=4;
for (int n=1; n<=threads; n++) {
int job;
if (n==1) {job = m * m;} else {job = (n-1)*upperBound/threads;}
int upToJob = n*upperBound/threads;
for (int k = job; k <= upToJob; k += m)
{
isComposite[k] = true;
}
}
}
}
for (int m = upperBoundSquareRoot; m <= upperBound; m++)
if (!isComposite[m])
System.out.print(m + " ");
}
I have created a loop for dividing work for 4 threads. Though I don't know how to make actual thread code from it. How to send variables and start 4 threads with part of job for each.
I can propose following solution: there are 4 workers thread and 1 master thread. Worker threads get jobs from queue. Job is basically 3 numbers: from, to, step. Master mean while must wait until all threads a done. When they're done it searches for next prime number and create 4 jobs. Synchronization between master and workers can be achieved using Semaphore: master tries to acquire 4 permits while every worker releases 1 permit when it's done.
public class Sieve {
// Number of workers. Make it static for simplicity.
private static final int THREADS = 4;
// array must be shared between master and workers threads so make it class property.
private boolean[] isComposite;
// Create blocking queue with size equal to number of workers.
private BlockingQueue<Job> jobs = new ArrayBlockingQueue<Job>(THREADS);
private Semaphore semaphore = new Semaphore(0);
// Create executor service in order to reuse worker threads.
// we can use just new Thread(new Worker()).start(). But using thread pools more effective.
private ExecutorService executor = Executors.newFixedThreadPool(THREADS);
public void runEratosthenesSieve(int upperBound) {
int upperBoundSquareRoot = (int) Math.sqrt(upperBound);
isComposite = new boolean[upperBound + 1];
// Start workers.
for (int i = 0; i < THREADS; i++) {
executor.submit(new Worker());
}
for (int m = 2; m <= upperBoundSquareRoot; m++) {
if (!isComposite[m]) {
System.out.print(m + " ");
for (int n=1; n<= THREADS; n++) {
int from;
if (n == 1) {
from = m * m;
} else {
from = (n-1)*upperBound/THREADS;
}
Job job = new Job(from, n*upperBound/threads, m);
// Submit job to queue. We don't care which worker gets the job.
// Important only that only 1 worker get the job. But BlockingQueue does all synchronization for us.
jobs.put(job);
}
// Wait until all jobs are done.
semaphore.acquire(THREADS);
}
}
for (int i = 0; i < n; i++) {
// put null to shutdown workers.
jobs.put(null);
}
for (int m = upperBoundSquareRoot; m <= upperBound; m++) {
if (!isComposite[m]) {
System.out.print(m + " ");
}
}
}
private class Job {
public int from, to, step;
public Job(int from, int to, int step) {
this.from = from;
this.to = to;
this.step = step;
}
}
private Worker implements Runnable {
while (true) {
Job job = jobs.take();
// null means workers must shutdown
if (job == null) {
return;
}
for (int i = job.from; i <= job.to; i += job.step) {
isComposite[i] = true;
}
// Notify master thread that a job was done.
semaphore.release();
}
}
}

Java MultiThreading objects

I am trying to do a modelling for some algorithms in java, what i am facing now is i need to run the main of the algorithm 10 times but the process takes 120 minutes to finish so i am doing each run on a thread. What i want is to create 10 threads without repeating the same code in each thread so how to make 10 different threads with the same code to excute. any ideas.
package biodavidcorne;
import java.util.Random;
/**
*
* #author hyder
*/
public class BIODavidCorne extends Thread {
public void run(int Runs) {
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
BIODavidCorne test = new BIODavidCorne();
Thread t1 = new Thread(new Runnable() {
#Override
public void run() {
try {
int Runs = 0;
int[][] Mean10Runs = new int[10][10000];
int[][] Min10Runs = new int[10][10000];
int[][] Max10Runs = new int[10][10000];
// for (int Runs = 0; Runs < 10; Runs++) {
BinList test = new BinList();
Random generator = new Random();
for (int i = 0; i < 10; i++) {
test.ReadLine("File.txt", i);
}
//test.PrintListOfGarbage();
for (int i = 0; i < 10; i++) {
test.InsertGarbageToBin(i);
}
for (int Big = 0; Big < 10000; Big++) {
int Mean = 0;
for (int x = 0; x < 10; x++) {
for (int i = 0; i < 50; i++) {
test.GetPenalties(x, i);
}
}
// System.out.println("*******************************************************************************************" + Big + " .. " + Runs);
// test.PrintListOfGarbage();
int[] penalty = new int[10];
int[] minimum = new int[10];
int[] maximum = new int[10];
int[] mutation = new int[10];
// test.PrintListOfGarbage();
for (int i = 0; i < 10; i++) {
penalty[i] = test.getAllPanalties(i);
}
for (int i = 0; i < 10; i++) {
minimum[i] = test.getMinimum(i);
maximum[i] = test.getMaximum(i);
mutation[i] = test.calculateMutation(penalty[i], minimum[i], maximum[i]);
//
}
int r = generator.nextInt(10);
int s = generator.nextInt(10);
test.MakeTheFitness(mutation, r, s);
test.resetPenaltyArray();
// test.PrintListOfGarbage();
for (int i = 0; i < 10; i++) {
Mean = Mean + mutation[i];
}
int min = mutation[0];
int max = 0;
for (int i = 0; i < 10; i++) {
if (min > mutation[i]) {
min = mutation[i];
}
if (max < mutation[i]) {
max = mutation[i];
}
}
Min10Runs[Runs][Big] = min;
Max10Runs[Runs][Big] = max;
Mean10Runs[Runs][Big] = (Mean / 10);
System.out.println("This is the Mean 1"+Big+".."+Runs);
}
System.out.println("This is the Mean + for Runs" + Runs + ".. " + Mean10Runs[Runs][9999] + "This is the Minimum " + Min10Runs[Runs][9999]);
} catch (Exception e) {
System.out.println("Not supported yet." + e);
}
}
});
Thread t2 = new Thread(new Runnable() {
#Override
public void run() {
try {
int Runs = 0;
int[][] Mean10Runs = new int[10][10000];
int[][] Min10Runs = new int[10][10000];
int[][] Max10Runs = new int[10][10000];
// for (int Runs = 0; Runs < 10; Runs++) {
BinList test = new BinList();
Random generator = new Random();
for (int i = 0; i < 10; i++) {
test.ReadLine("File.txt", i);
}
//test.PrintListOfGarbage();
for (int i = 0; i < 10; i++) {
test.InsertGarbageToBin(i);
}
for (int Big = 0; Big < 10000; Big++) {
int Mean = 0;
for (int x = 0; x < 10; x++) {
for (int i = 0; i < 50; i++) {
test.GetPenalties(x, i);
}
}
// System.out.println("*******************************************************************************************" + Big + " .. " + Runs);
// test.PrintListOfGarbage();
int[] penalty = new int[10];
int[] minimum = new int[10];
int[] maximum = new int[10];
int[] mutation = new int[10];
// test.PrintListOfGarbage();
for (int i = 0; i < 10; i++) {
penalty[i] = test.getAllPanalties(i);
}
for (int i = 0; i < 10; i++) {
minimum[i] = test.getMinimum(i);
maximum[i] = test.getMaximum(i);
mutation[i] = test.calculateMutation(penalty[i], minimum[i], maximum[i]);
//
}
int r = generator.nextInt(10);
int s = generator.nextInt(10);
test.MakeTheFitness(mutation, r, s);
test.resetPenaltyArray();
// test.PrintListOfGarbage();
for (int i = 0; i < 10; i++) {
Mean = Mean + mutation[i];
}
int min = mutation[0];
int max = 0;
for (int i = 0; i < 10; i++) {
if (min > mutation[i]) {
min = mutation[i];
}
if (max < mutation[i]) {
max = mutation[i];
}
}
Min10Runs[Runs][Big] = min;
Max10Runs[Runs][Big] = max;
Mean10Runs[Runs][Big] = (Mean / 10);
System.out.println("This is the Mean 2"+Big+".."+Runs);
}
} catch (Exception e) {
System.out.println("Not supported yet." + e);
}
}
});
t1.start();
t2.start();
}
}
Just make the Runnable a named class instead of an anonymous inner class, and reuse it:
class MyRunnable implements Runnable {
#Override
public void run() {
int Runs = 0;
int[][] Mean10Runs = new int[10][10000];
// ...
new Thread(new MyRunnable()).start();
You can (and should) put MyRunnable in its own source file.
There is no reason to copy and paste the content of the anonymous inner class ten times. Just assign it to a variable and use it 10 times.
Runnable runnable = new Runnable() {
#Override
public void run() {
// ... the code in the anonymous inner class
}
}
// Start 10 threads with this code
for (int i = 0; i < 10; ++i) {
new Thread(runnable).start();
}
You can use a for loop to create the threads.
Also, if you want to ensure that all of the threads kick off at the same time, you can use a CyclicBarrier, but if not you can just start each thread as you create it.
If you need to hold a reference to the threads, store them in a Thread array
CyclicBarrier barrier = new CyclicBarrrier(10);
// Runnable runnable = ... your code. at the beginning of the Runnable put the first line
barrier.await(); // and catch the exception
Thread[] threads = new Thread[10];
for(int i = 0; i < 10; i++){
threads[i] = new Thread(runnable);
threads[i].start();
}
That's it! Good luck - let me know if you need help implementing the runnable
Unless you have 10 CPU cores do not spawn 10 threads.
You can use commons threadpool and set it the size of the number of cpu cores, so you can run the tasks in parallel and sequentially.
try using a ThreadPoolExecutor
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ThreadPoolExecutor.html
Create a class that extends Thread and implement the run method. You could pass argument through the constructor, keep them as members to use them in the run() method.
Something like this:
public class YourThread extends Thread{
private String mParam1;
private Object mParam2;
public YourThread( String param1, Object param2 ){
mParam1 = param1;
mParam2 = param2;
}
public void run(){
// do your stuff here
// ...
}
}
In the other class:
YourThread t1 = new Thread( "toto", new Object() );
t1.start();
YourThread t2 = new Thread( "titi", new Object() );
t2.start();

Categories

Resources