1.编写一个程序,启动两个流,并在10秒内显示它们的名称和当前时间。2.编写一个程序,该程序启动流并显示1到10之间的数字,输出之间的延迟为1秒。3.编写一个程序,创建两个显示从1到10的数字的流。第一个流只能输出偶数,第二个流只能输出奇数。4.编写一个程序,创建10个流,每个流在屏幕上显示其编号。5.编写一个函数,使用多线程查找整数数组中的最大元素。线程数必须等于处理器核心数。6.编写一个函数,该函数使用多线程总结整数数组中的元素。线程数必须等于处理器核心数。
1。
import java.time.LocalDateTime;
public class H1{
public static void main(String[] args) {
Thread t1 = new Thread(() -> System.out.println("Stream 1: " + LocalDateTime.now()));
Thread t2 = new Thread(() -> System.out.println("Stream 2: " + LocalDateTime.now()));
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class H1{
public static void main(String[] args) {
Thread t1 = new Thread(() -> System.out.println("Stream 1: " + LocalDateTime.now()));
Thread t2 = new Thread(() -> System.out.println("Stream 2: " + LocalDateTime.now()));
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2.
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
public class H2 {
public static void main(String[] args) {
IntStream.rangeClosed(1, 10)
.forEach(i -> {
System.out.println(i);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
3.
import java.util.stream.IntStream;
public class H2 {
public static void main(String[] args) {
IntStream.rangeClosed(1, 10)
.forEach(i -> {
System.out.println(i);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
3.
import java.util.stream.IntStream;
public class H3{
public static void main(String[] args) {
IntStream.rangeClosed(1, 10)
.filter(i -> i % 2 == 0)
.forEach(System.out::println);
IntStream.rangeClosed(1, 10)
.filter(i -> i % 2 == 1)
.forEach(System.out::println);
}
}
public class H3{
public static void main(String[] args) {
IntStream.rangeClosed(1, 10)
.filter(i -> i % 2 == 0)
.forEach(System.out::println);
IntStream.rangeClosed(1, 10)
.filter(i -> i % 2 == 1)
.forEach(System.out::println);
}
}
4.
public class H4 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
int streamNumber = i;
new Thread(() -> System.out.println("Stream " + streamNumber)).start();
}
}
}
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
int streamNumber = i;
new Thread(() -> System.out.println("Stream " + streamNumber)).start();
}
}
}
5.
import java.util.Arrays;
public class H5 {
public static void main(String[] args) {
int[] array = {1, 6, 2, 9, 3, 7, 4, 8, 5, 0};
int processorCount = Runtime.getRuntime().availableProcessors();
Thread[] threads = new Thread[processorCount];
int[] results = new int[processorCount];
for (int i = 0; i < processorCount; i++) {
int startIndex = i * (array.length / processorCount);
int endIndex = (i == processorCount - 1) ? array.length : (i + 1) * (array.length / processorCount);
int finalI = i;
threads[i] = new Thread(() -> {
results[finalI] = Arrays.stream(array, startIndex, endIndex).max().orElse(Integer.MIN_VALUE);
System.out.println(Thread.currentThread().getName() + " found max value of " + results[finalI]);
});
threads[i].start();
}
try {
for (Thread thread : threads) {
thread.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
int maxValue = Arrays.stream(results).max().orElse(Integer.MIN_VALUE);
System.out.println("Max value is " + maxValue);
}
}
public class H5 {
public static void main(String[] args) {
int[] array = {1, 6, 2, 9, 3, 7, 4, 8, 5, 0};
int processorCount = Runtime.getRuntime().availableProcessors();
Thread[] threads = new Thread[processorCount];
int[] results = new int[processorCount];
for (int i = 0; i < processorCount; i++) {
int startIndex = i * (array.length / processorCount);
int endIndex = (i == processorCount - 1) ? array.length : (i + 1) * (array.length / processorCount);
int finalI = i;
threads[i] = new Thread(() -> {
results[finalI] = Arrays.stream(array, startIndex, endIndex).max().orElse(Integer.MIN_VALUE);
System.out.println(Thread.currentThread().getName() + " found max value of " + results[finalI]);
});
threads[i].start();
}
try {
for (Thread thread : threads) {
thread.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
int maxValue = Arrays.stream(results).max().orElse(Integer.MIN_VALUE);
System.out.println("Max value is " + maxValue);
}
}
6.
import java.util.Arrays;
public class H6 {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int processorCount = Runtime.getRuntime().availableProcessors();
Thread[] threads = new Thread[processorCount];
int[] results = new int[processorCount];
for (int i = 0; i < processorCount; i++) {
int startIndex = i * (array.length / processorCount);
int endIndex = (i == processorCount - 1) ? array.length : (i + 1) * (array.length / processorCount);
threads[i] = new Thread(() -> {
results[i] = Arrays.stream(array, startIndex, endIndex).sum();
System.out.println(Thread.currentThread().getName() + " summed up to " + results[i
public class H6 {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int processorCount = Runtime.getRuntime().availableProcessors();
Thread[] threads = new Thread[processorCount];
int[] results = new int[processorCount];
for (int i = 0; i < processorCount; i++) {
int startIndex = i * (array.length / processorCount);
int endIndex = (i == processorCount - 1) ? array.length : (i + 1) * (array.length / processorCount);
threads[i] = new Thread(() -> {
results[i] = Arrays.stream(array, startIndex, endIndex).sum();
System.out.println(Thread.currentThread().getName() + " summed up to " + results[i