/*
- Thread()常用的方法:
- 1.start():启动线程并执行相应的run()方法
- 2.run():子线程要执行的代码放入run()方法中
- 3.currentThread():调取当前的线程
- 4.getName():获取线程的方法
- 5.setName():设置此线程的方法
- 6.yield():调用此方法的线程释放当前CPU的执行权
- 7.join():在A线程中调用B线程的join()方法,表示:当执行到此方法,A线程停止执行,直至B线程执行完毕
- 8.sleep(long l);显示的让当前线程睡眠一毫秒
- 9.isAilve():判断当前线程是否还存活
- 10.线程通信:wait() notify() notifyall()
- 设置线程的优先级
- getPriority()获取线程的优先值
- setPriority() 改变线程的优先值
*/
class SubThread extends Thread{
public void run() {
for(int i = 1;i <= 100;i++) {
// try {
// sleep(0);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
System.out.println(Thread.currentThread().getName() + ":" +i);
}
}
}
public class TestThread {
public static void main(String[] args) {
SubThread st = new SubThread();
st.setPriority(1);
st.start();
Thread.currentThread().setPriority(10);
for(int i = 1;i <= 100;i++) {
System.out.println(Thread.currentThread().getName() + ":" +i);
if(i % 10 == 0) {
Thread.currentThread().yield();
}
// if(i == 20) {
// try {
// st.join();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
System.out.println(st.isAlive());
}
}
}