Skip to content

Commit

Permalink
Complete multithreading java task
Browse files Browse the repository at this point in the history
  • Loading branch information
JackKaif committed Feb 21, 2024
1 parent 6eb9796 commit c76f115
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 2 additions & 0 deletions java-advanced-ru/multithreading-java/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.gradle/
build/
caches/
.idea/
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package exercise;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import java.util.logging.Level;
Expand All @@ -8,6 +9,23 @@ class App {
private static final Logger LOGGER = Logger.getLogger("AppLogger");

// BEGIN

public static Map<String, Integer> getMinMax(int[] array) {
var maxThread = new MaxThread(array);
var minThread = new MinThread(array);
var result = new HashMap<String, Integer>();
maxThread.start();
minThread.start();
try {
maxThread.join();
minThread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
var max = maxThread.getMax();
var min = minThread.getMin();
result.put("max", max);
result.put("min", min);
return result;
}
// END
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package exercise;

import java.util.Arrays;

// BEGIN
public class MaxThread extends Thread{
private final int[] array;

private int max;

public MaxThread(int[] array) {
this.array = array;
}
@Override
public void run() {
System.out.println("Thread " + this.getName() + " started");
max = Arrays.stream(array)
.max()
.orElse(0);
System.out.println("Thread " + this.getName() + " finished");
}

public int getMax() {
return max;
}
}
// END
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package exercise;

import java.util.Arrays;

// BEGIN
public class MinThread extends Thread {
private final int[] array;

private int min;

public MinThread(int[] array) {
this.array = array;
}
@Override
public void run() {
System.out.println("Thread " + this.getName() + " started");
min = Arrays.stream(array)
.min()
.orElse(0);
System.out.println("Thread " + this.getName() + " finished");
}

public int getMin() {
return min;
}
}
// END

0 comments on commit c76f115

Please sign in to comment.