package com.yyx.test;
public class TestThread {
public static void main(String[] args) {
ThreadDemo thread1 = new ThreadDemo();
thread1.setName("线程一");
ThreadDemo thread2 = new ThreadDemo();
thread2.setName("线程二");
thread1.start();
thread2.start();
}
}
class ThreadDemo extends Thread {
@Override
public void run() {
for (int i = 1; i <= 20; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
package com.yyx.test;
public class TestThread {
public static void main(String[] args) {
ThreadDemo thread = new ThreadDemo();
thread.setName("线程一");
thread.start();
for(int j=1;j<=20;j++) {
Thread.currentThread().setName("主线程");
System.out.println(Thread.currentThread().getName() + ":" + j);
if(j==10) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class ThreadDemo extends Thread {
@Override
public void run() {
for (int i = 1; i <= 20; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}