定义两条线程,一条线程负责输出数字,另一条线程负责输出字母,
要求两条线程同时启动,打印出如下效果:
12A34B56C78D…5152Z
关键点 用main线程的sleep错开两个线程
。
public class Subject01 {
static boolean flag=false;
/**
* @author blue
* @date 2020年8月11日
*/
public static void main(String[] args) {
Thread t1=new NumberThread();
Thread t2=new LetterThread();
t1.start();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t2.start();
}
}
//子母线程
class LetterThread extends Thread{
char letter=65;
@Override
public void run() {
for(int i=0;i<26;i++) {
System.out.print(letter++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//数字线程
class NumberThread extends Thread{
int num=1;
@Override
public void run() {
for (int i = 1; i <= 26; i++) {
System.out.print(num++ +""+num++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}