setName//设置线程名称
getName
start
run
setPriority//更改线程优先级
getPriority//获取线程优先级
sleep
interrupt//中断线程
package thread_;
public class ThreadMethod {
public static void main(String[] args) throws InterruptedException {
TT t = new TT();
t.setName("Jack");//设置线程名称
t.setPriority(Thread.MIN_PRIORITY);//优先级最低
t.start();// 启动子线程
//主线程输出,然后中断子线程休眠
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+"主线程工作中");
Thread.sleep(1000);
}
t.interrupt();//中断子线程t的休眠
}
}
class TT extends Thread{
@Override
public void run() {
while (true){
for (int i= 0;i<100;i++){
//
System.out.println(Thread.currentThread().getName()+"在工作"+i);
}
try {
System.out.println(Thread.currentThread().getName()+"休憩中");
Thread.sleep(10000);
} catch (InterruptedException e) {
//线程若在休眠过程中被interrupt中断,则会进入catch里面执行相应语句
System.out.println(Thread.currentThread().getName()+"被打断");
}
}
}
}