目录
常用方法API
API测试:
MyThread类:
package com.itheima.api;
public class MyThread extends Thread{
public MyThread(String name) {
// 设置当前对象名称
super(name);
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+"子线程输出"+i);
}
}
}
Demo1测试:
package com.itheima.api;
public class Demo1 {
public static void main(String[] args) {
Thread t1 = new MyThread("1号");
// t1.setName("1号");
t1.start();
Thread t2 = new MyThread("2号");
// t2.setName("2号");
t2.start();
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+"主线程输出"+i);
}
}
}
Demo1输出结果:
Demo2:
package com.itheima.api;
public class Demo2 {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 5; i++) {
System.out.println(i);
if(i==2){
Thread.sleep(5000);
}
}
}
}
Demo2输出结果:
Ending...
我是小天,路虽远 行必至,各位的喜爱是对我最大的鼓励,我们下篇文章见~
......