这一段多线程代码
public class ThreadExample {
public static void main(String[] args) {
// Create two Runnable objects
Runnable task1 = new Task1();
Runnable task2 = new Task2();
// Create two threads using the Runnables
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
// Start the threads
thread1.start();
thread2.start();
}
private static class Task1 implements Runnable {
@Override
public void run() {
for (int i=0; i<5; i++) {
System.out.println("Task 1: " + i);
try {
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException e) {}
}
}
}
private static class Task2 implements Runnable{
@Override
public void run() {
for (int i=0;i<5;i++) {
System.out.println("Task 2: "+i);
try{ Thread.sleep(1000);//Sleep for 1 second }catch (InterruptedException e){}
} } } }
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。