Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class TenThreads {
- private static class WorkerThread implements Runnable {
- Thread t;
- int max = Integer.MIN_VALUE;
- int[] ourArray;
- public WorkerThread(int[] ourArray) {
- this.ourArray = ourArray;
- }
- @Override// Find the maximum value in our particular piece of the array
- public void run() {
- for (int i = 0; i < ourArray.length; i++)
- max = Math.max(max, ourArray[i]);
- }
- public int getMax() {
- return max;
- }
- public void start()
- {
- t=new Thread(this);
- t.start();
- }
- }
- public static void main(String[] args) {
- WorkerThread[] threads = new WorkerThread[20];
- int[][] bigMatrix = getBigHairyMatrix();
- int max = Integer.MIN_VALUE;
- // Give each thread a slice of the matrix to work with
- for (int i = 0; i < 20; i++) {
- threads[i] = new WorkerThread(bigMatrix[i]);
- threads[i].start();
- }
- // Wait for each thread to finish
- try {
- for (int i = 0; i < 20; i++) {
- new Thread(threads[i]).join();
- // threads[i].join(); // Ova se koristi za da chekaat drugite nitki da zavrshi ednata za da pochne
- // drugata po nea, bez nea nema redosled koja bi zavrshila prva, vtora, itn.
- max = Math.max(max, threads[i].getMax());
- }
- } catch (InterruptedException e) {
- // fall through
- }
- System.out.println("Maximum value was " + max);
- }
- static int[][] getBigHairyMatrix() {
- int x = 100;
- int y = 100;
- int[][] matrix = new int[x][y];
- Random rnd = new Random();
- for (int i = 0; i < x; i++)
- for (int j = 0; j < y; j++) {
- matrix[i][j] = rnd.nextInt();
- }
- return matrix;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement