System.out.println("main:"+Thread.currentThread().getName());
}
}
}
class MyThread extends Thread...{
public void run()...{
while(true)...{
System.out.println(getName());
}
}
}
System.out.println("main:"+Thread.currentThread().getName());
}
}
}
class MyThread extends Thread...{
public void run()...{
while(true)...{
System.out.println(getName());
}
}
}编译运行:

可见,当主函数方法所在线程终止时,后台线程也会终止执行。
当我们在run()中打印完线程名称时,再执行语句yield();则让出执行时间片,运行结果:

可以设置和获取线程的优先级:
setPriority(int newPriority) ;
getPriority() ;
Thread类定义了三个优先级常量:
MAX_PRIORITY 最大优先级10
MIN_PRIORITY 最小优先级1
NORM_PRIORITY 默认优先级5
改变一个线程的优先级:
mt.setPriority(Thread.MAX_PRIORITY);
查看运行情况:
(此时如果yield();仍然被执行)

如果删除yield();语句,则:

只有Thread-0在执行。
也可以去实现Runnable类的接口:

class MultiThread...{
public static