public class threadtest1 {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("thread 1:" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("thread 2:" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("thread 3:" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread1.start();
thread1.join();
thread2.start();
thread2.join();
thread3.start();
System.out.println("Hello World");
}
}
输出:
thread 1:0
thread 1:1
thread 1:2
thread 1:3
thread 1:4
thread 2:0
thread 2:1
thread 2:2
thread 2:3
thread 2:4
Hello World
thread 3:0
thread 3:1
thread 3:2
thread 3:3
thread 3:4
public class threadtest2 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// 线程1
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("thread 1:" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
// 线程2
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("thread 2:" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread1.start();
try {
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
thread2.start();
}
});
thread.start();
System.out.println("Hello World");
}
}
输出:
Hello World
thread 1:0
thread 1:1
thread 1:2
thread 1:3
thread 1:4
thread 2:0
thread 2:1
thread 2:2
thread 2:3
thread 2:4
public class threadtest3 {
public static void main(String[] args) throws InterruptedException {
for (int j = 0; j < 3; j++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// 线程1
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("thread 1:" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
// 线程2
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("thread 2:" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread1.start();
try {
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
thread2.start();
}
});
thread.start();
}
System.out.println("Hello World");
}
}
输出:
Hello World
thread 1:0
thread 1:0
thread 1:0
thread 1:1
thread 1:1
thread 1:1
thread 1:2
thread 1:2
thread 1:2
thread 1:3
thread 1:3
thread 1:3
thread 1:4
thread 1:4
thread 1:4
thread 2:0
thread 2:0
thread 2:0
thread 2:1
thread 2:1
thread 2:1
thread 2:2
thread 2:2
thread 2:2
thread 2:3
thread 2:3
thread 2:3
thread 2:4
thread 2:4
thread 2:4