public class thread1 extends thread {
@override
public void run() {
for (int i = 0; i < 10; i++) {
system.out.println(getname() + ": " + i);
}
}
public static void main(string[] args) {
for (int i = 0; i < 10; i++) {
system.out.println(thread.currentthread().getname() + ": " + i);
if (i == 2) {
new thread1().start();
new thread1().start();
}
}
}
}
实现runnable接口
public class thread2 implements runnable {
@override
public void run() {
for (int i = 0; i < 10; i++) {
system.out.println(thread.currentthread().getname() + ": " + i);
}
}
public static void main(string[] args) {
for (int i = 0; i < 10; i++) {
system.out.println(thread.currentthread().getname() + ": " + i);
if (i == 2) {
thread2 thread2 = new thread2();
new thread(thread2).start();
new thread(thread2).start();
}
}
}
}