float[][] matrix = new float[N][N];
for (int r = 0, n = 0; r < matrix.length; ++r)
for (int c = 0; c < matrix[r].length; ++c) matrix[r][c] = ++n;
return matrix;
}
private static void print(float[][] matrix) {
for (int r = 0; r < matrix.length; ++r) {
for (int c = 0; c < matrix[r].length; ++c) System.
out.printf("%10.4f", matrix[r][c]);
System.out.println();
} } }
class Solver {
private final float[][] data; // matricen
private final CyclicBarrier barrier; // synkroniserer flere trådes arbejde
TomTom is a place for people who see solutions when faced with problems, who have the energy to drive our technology, innovation, growth along with goal achievement. We make it easy for people to make smarter decisions to keep moving towards their goals. If you share our passion - this could be the place for you.
Founded in 1991 and headquartered in Amsterdam, we have 3,600 employees worldwide and sell our products in over 35 countries.
We ask you WHERE DO YOU
WANT TO BE?
JAVA 8:MULTITHREADED PROGRAMS
58
ConCurrenCy tools JAVA 8:MULTITHREADED PROGRAMS
58
COnCURREnCy TOOLS
public Solver(float[][] matrix) {
data = matrix;
barrier =
new CyclicBarrier(matrix.length, new Runnable() { public void run() { merge(); } });
for (int r = 0; r < matrix.length; ++r) new Thread(new Worker(r)).start();
synchronized("abc") {
try {
System.out.println("[" +Thread.currentThread().getId() + "] waiting");
"abc".wait();
System.out.println("[" +Thread.currentThread().getId() + "] notified");
}
catch (InterruptedException ie) {
System.out.println("main thread interrupted");
} } }
void merge() {
System.out.println("merging");
synchronized("abc") {
"abc".notify();
} }
class Worker implements Runnable {
private final int row; // the row that the thread works on private boolean done = false; // when the thread has to stop public Worker(int row)
{
this.row = row;
}
public boolean done() {
return done;
}
JAVA 8:MULTITHREADED PROGRAMS ConCurrenCy tools
JAVA 8:MULTITHREADED PROGRAMS COnCURREnCy TOOLS
private void work() {
System.out.println("Behandler række: " + row);
for (int i = 0; i < data[row].length; ++i)
The method create() creates the matrix and the method print() prints the matrix on the screen. Both of these methods are trivial. After the matrix has been created, is instantiated an object of the type Solver, and it is the object which solves the problem. The class Solver creates a CyclicBarrier which is initialized with a number of worker threads, and a runnable object to be performed when all worker threads have completed their work. In this case, the object’s run() method calls the method merge(). After the worker threads are created – one thread for each row – and the threads are created on basis of a runnable object of the type Worker. The class is basically trivial, but it is an inner class (in the class Solver) and therefore it knows the synchronization object barrier, and after it has done its job, it performs a
barrier.await();
which means that the thread is waiting at the barrier. After the constructor in the class Solver has started the worker threads, it performs wait() on a lock (which is simply a string), and it means that the primary thread waits until it gets a notify from another thread.
The method create() creates the matrix and the method print() prints the matrix on the screen. Both of these methods are trivial. After the matrix has been created, is instantiated an object of the type Solver, and it is the object which solves the problem. The class Solver creates a CyclicBarrier which is initialized with a number of worker threads, and a runnable object to be performed when all worker threads have completed their work. In this case, the object’s run() method calls the method merge(). After the worker threads are created – one thread for each row – and the threads are created on basis of a runnable object of the type Worker. The class is basically trivial, but it is an inner class (in the class Solver) and therefore it knows the synchronization object barrier, and after it has done its job, it performs a
JAVA 8:MULTITHREADED PROGRAMS COnCURREnCy TOOLS
private void work() {
System.out.println("Behandler række: " + row);
for (int i = 0; i < data[row].length; ++i)
The method create() creates the matrix and the method print() prints the matrix on the screen. Both of these methods are trivial. After the matrix has been created, is instantiated an object of the type Solver, and it is the object which solves the problem. The class Solver creates a CyclicBarrier which is initialized with a number of worker threads, and a runnable object to be performed when all worker threads have completed their work. In this case, the object’s run() method calls the method merge(). After the worker threads are created – one thread for each row – and the threads are created on basis of a runnable object of the type Worker. The class is basically trivial, but it is an inner class (in the class Solver) and therefore it knows the synchronization object barrier, and after it has done its job, it performs a
barrier.await();
which means that the thread is waiting at the barrier. After the constructor in the class Solver has started the worker threads, it performs wait() on a lock (which is simply a string), and it means that the primary thread waits until it gets a notify from another thread.
which means that the thread is waiting at the barrier. After the constructor in the class Solver has started the worker threads, it performs wait() on a lock (which is simply a string), and it means that the primary thread waits until it gets a notify from another thread.
JAVA 8:MULTITHREADED PROGRAMS
60
ConCurrenCy tools
60
After all worker threads are performed an await() on the barrier (all threads have reached the barrier), the barrier object let the threads continue (which here simply means that they stop) and carry out its own thread, which here calls the merge() method. It does not much, but it sends a notify() on the lock set by the primary thread, and the result is that the primary thread continues.
If you executes the program, you get the result:
JAVA 8:MULTITHREADED PROGRAMS
60
COnCURREnCy TOOLS
60
After all worker threads are performed an await() on the barrier (all threads have reached the barrier), the barrier object let the threads continue (which here simply means that they stop) and carry out its own thread, which here calls the merge() method. It does not much, but it sends a notify() on the lock set by the primary thread, and the result is that the primary thread continues.
If you executes the program, you get the result:
1,0000 2,0000 3,0000 4,0000 5,0000 6,0000 7,0000 8,0000 9,0000 10,0000 11,0000 12,0000 13,0000 14,0000 15,0000 16,0000 17,0000 18,0000 19,0000 20,0000 21,0000 22,0000 23,0000 24,0000 25,0000 Behandler række: 0
Behandler række: 1 Behandler række: 2 Behandler række: 3 [1] waiting
Behandler række: 4 merging
[1] notified
Deutsche Bank db.com/careers