package Helloworld.exer;
/*
测试Thread中的常用方法:
1 start():启动当前线程:调用当前线程的run()
2 run():通常需要重写Thread类中的此方法,将创建的线程要执行的操作声明在此方法中
3 currentThread():静态方法:执行当前代码的线程
4 getName():获取当前线程的名字
5 setName():设置当前线程的名字
6 yield();释放当前cpu的执行权
7 join:在线程a中调用线程b的join方法,那么线程a就进入阻塞状态直到线程b完全执行完以后,线程啊
才结束阻塞状态。
8 STOP():已过时。当执行此方法时,强行结束当前线程
9sleep();
10 isAlive()判断当前线程是否存活
线程的优先级:
1.MAX_PRIORITY 10
MIN_PRIORITY 1
NORM_PRIORITY 5
如何获取和设置当前线程的优先级:
gerpriority()获取线程优先级
setpriority(int p)设置线程优先级-->默认的优先级
/
*/
class th extends Thread{
public void run() {
for(int i=0;i<100;++i){
try{
final int i1 = 55;
sleep(1);
}catch(InterruptedException e){
e.printStackTrace();
}
if(i%3==0){
System.out.println(currentThread().getName()+i);
}
if(i%2==0){
yield();
}
}
}
public th(String name){
super(name);
}
}
public class ThreadMethodTest {
public static void main(String[] args) {
th t1=new th("八重神子");
//设置分线程的优先级
t1.setPriority(Thread.MAX_PRIORITY);
Thread.currentThread().getPriority();
t1.getPriority();
Thread.currentThread().setName("主线程");
t1.start();
System.out.println(1);
for(int i=0;i<100;++i){
if(i%3==0){
System.out.println(i);
if(i==3){
try{
t1.join();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
System.out.println(t1.isAlive());
}
}
为什么主线程的名字没有赋上去。前面还是没有线程名字。