lr12 -> e2-6
This commit is contained in:
25
lr12/e2.java
Normal file
25
lr12/e2.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package lr12;
|
||||
|
||||
public class e2 {
|
||||
public static void main(String[] args) {
|
||||
Thread t1 = new Thread(() -> {
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
System.out.println(i);
|
||||
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
t1.start();
|
||||
|
||||
try {
|
||||
t1.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
24
lr12/e3.java
Normal file
24
lr12/e3.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package lr12;
|
||||
|
||||
public class e3 {
|
||||
public static void main(String[] args) {
|
||||
Thread evenThread = new Thread(() -> {
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
if (i % 2 == 0) {
|
||||
System.out.println("Even: " + i);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Thread oddThread = new Thread(() -> {
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
if (i % 2 != 0) {
|
||||
System.out.println("Odd: " + i);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
evenThread.start();
|
||||
oddThread.start();
|
||||
}
|
||||
}
|
||||
15
lr12/e4.java
Normal file
15
lr12/e4.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package lr12;
|
||||
|
||||
public class e4 {
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
final int threadNumber = i;
|
||||
|
||||
Thread t = new Thread(() -> {
|
||||
System.out.println("Thread # " + threadNumber);
|
||||
});
|
||||
|
||||
t.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
21
lr12/e5.java
Normal file
21
lr12/e5.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package lr12;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class e5 {
|
||||
public static int findMax(int[] arr) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
throw new IllegalArgumentException("Empty");
|
||||
}
|
||||
|
||||
return Arrays.stream(arr)
|
||||
.parallel()
|
||||
.max()
|
||||
.getAsInt();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] arr = { 3, 8, 1, 99, 4, 12, 7, 45, -5, 88 };
|
||||
System.out.println("Max: " + findMax(arr));
|
||||
}
|
||||
}
|
||||
20
lr12/e6.java
Normal file
20
lr12/e6.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package lr12;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class e6 {
|
||||
public static long sum(int[] arr) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Arrays.stream(arr)
|
||||
.parallel()
|
||||
.sum();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 128, 512 };
|
||||
System.out.println("Sum = " + sum(array));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user