线程
实现
1.实现runnable方法,并在创建线程时候传入构造器中
2.继承thread,重写run方法
最后执行线程start方法,就会启动线程
注意线程中断最好不要使用stop方法
另外注意,如果线程设置为守护线程 ,那么守护的线程结束了,当前线程也会结束,必须在start方法之前设置
public class Main {
static class T extends Thread{
public T(String name) {
super(name);
}
@Override
public void run() {
while(true){
try {
currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(111);
}
}
}
public static void main(String[] args) throws InterruptedException {
T t = new T("test---001");
//t.setDaemon(true);
t.start();
Thread.currentThread().sleep(5000);
System.out.println("over");
}
}
只是守护线程注释,会发现over以后线程t就停住了,没注释就一直运行
通过jstack指令观察

Last updated
Was this helpful?