当前位置:课程学习>>第十章 多线程>>学习内容>>知识点二
同学们,请运用你学到的知识,尝试分析下面的案例。
案例:编写一个使用继承Thread类的方法实现多线程的程序。该类有两个属性,一个字符串代表线程名,一个整数代表该线程要休眠的时间。线程执行时,显示线程名和休眠时间。
本题主要考察了线程的创建和线程sleep()方法的使用。
参考代码如下:
public class testThread2 extends Thread {
private int s;
private String info;
testThread2(int m, String n) {
info = n;
s = m;
}
public void run() {
try {
Thread.sleep(s);
System.out.println(info + " sleep:" + s);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
testThread2 t = new testThread2(1000, "线程1");
testThread2 t2 = new testThread2(3000, "线程2");
testThread2 t3 = new testThread2(6000, "线程3");
t.start();
t2.start();
t3.start();
}
}