Java生成 代码提供接口可以自由创建多线程平摊 n 次调用日志输出(例如 5 个线程 平 摊 100 次,每个线程要调用 20 次)
时间: 2023-02-19 14:14:09 浏览: 53
Java代码可以通过ExecutorService类实现多线程平摊n次调用,可以创建一个固定大小的线程池,并将每次调用拆分为多个任务,然后使用ExecutorService.submit()方法提交任务,并使用ExecutorService.shutdown()方法关闭线程池,最后使用日志输出输出相应的信息。
相关问题
java多线程编写一个程序,创建两个线程分别输出奇数和偶数。其中,每个线程循环输出10次
好的,这是一个简单的Java程序,用于创建两个线程分别输出奇数和偶数,每个线程循环输出10次:
```java
public class PrintNumbers {
public static void main(String[] args) {
Thread oddThread = new Thread(new PrintOdd());
Thread evenThread = new Thread(new PrintEven());
oddThread.start();
evenThread.start();
try {
oddThread.join();
evenThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static class PrintOdd implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 20; i += 2) {
System.out.println(Thread.currentThread().getName() + ": " + i);
}
}
}
static class PrintEven implements Runnable {
@Override
public void run() {
for (int i = 2; i <= 20; i += 2) {
System.out.println(Thread.currentThread().getName() + ": " + i);
}
}
}
}
```
在这个程序中,我们定义了两个内部类 `PrintOdd` 和 `PrintEven`,分别用于输出奇数和偶数。在每个类中,我们使用了 `for` 循环来生成一系列奇数和偶数,并使用 `System.out.println` 方法输出它们。注意,我们在输出中加入了当前线程的名称,以便区分两个线程的输出。
然后,在主函数中,我们创建了两个线程 `oddThread` 和 `evenThread`,分别用于执行 `PrintOdd` 和 `PrintEven` 类。我们使用 `start` 方法启动这两个线程,并使用 `join` 方法等待它们的完成。注意,我们需要等待两个线程完成后才能退出程序,否则可能会出现输出的混乱。
java 创建两个子线程 每个线程均输出20次
好的,以下是Java代码实现:
```java
public class TwoThreads {
public static void main(String[] args) {
Thread thread1 = new Thread(new PrintThread("Thread 1"));
Thread thread2 = new Thread(new PrintThread("Thread 2"));
thread1.start();
thread2.start();
}
}
class PrintThread implements Runnable {
private String name;
public PrintThread(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println(name + ": " + i);
try {
Thread.sleep(100); // 睡眠 100 毫秒,使线程交替输出
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
在这个例子中,我们依然使用了 `TwoThreads` 和 `PrintThread` 两个类。不同的是,在 `PrintThread` 类中,我们在输出每个数字后,让线程睡眠 100 毫秒,这样可以使线程交替输出。当然,这并不是唯一的实现方式,这里只是提供一种简单的方法。
当我们运行 `TwoThreads` 类时,会启动两个线程,每个线程都输出 0 到 19 的数字,并且两个线程的输出是交替进行的。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)