Java --Thread(1)

来源:互联网 发布:网络电影痞子兵王 编辑:程序博客网 时间:2024/06/03 12:29
/**
 * Thread类的常用方法:
 * 
 * 1.start():启动线程并执行相应的run()方法
 * 2.run():子线程要执行的代码,放入run()方法中
 * 3.currentThread():静态的、调取当前的线程
 * 4.getName():获取此线程的名字
 * 5.setName():设置此线程的名字
 * 6.yield():调用此方法的线程会释放当前CPU的执行权
 * 7.join():在A线程中调用B线程的join()方法,表示当执行到此方法,A线程停止执行,
 * 直至B线程执行完毕,A线程再接着join()之后的代码执行
 * 8.isAlive():判断当前线程是否还存活
 * 9.sleep(long 1):显式地让当前线程睡眠1毫秒
 * 10.线程通信:wait() notify() notifyAll()
 * 
 * 设置线程的优先级(线程的调度)
 * getPriority():返回线程优先值
 * setPriority(int newPriority):改变线程的优先级
 * 
 */
public class Test_Thread1 {
public static void main(String[] args) {
SubThread1 st = new SubThread1();

st.setName("子线程=");
st.setPriority(Thread.MAX_PRIORITY);
st.start();

Thread.currentThread().setName("主线程=====");
for (int i = 1; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" 
+ Thread.currentThread().getPriority() + ":" + i); 
// if(i % 10 == 0){
// Thread.currentThread().yield();
// }
// if(i == 20){
// try {
// st.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }

}
System.out.println(st.isAlive());
}


}
class SubThread1 extends Thread{
public void run(){
for(int i = 1; i <= 100 ; i++){
// try {
// Thread.currentThread().sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
System.out.println(Thread.currentThread().getName() + ":" 
+ Thread.currentThread().getPriority() + ":" + i);//1-100之间自然数输出

}
}

}