线程Thread-----方法(1)-----继承:


class SubThread extends  Thread{
    public void run(){
        for (int i = 0; i <50 ; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+Thread.currentThread().getPriority()+i);
        }
    }
}
class Test {
    public static void main(String[] args) {
        SubThread subThread = new SubThread();

        subThread.setName("子线程");//setName:设置此线程的名字

        subThread.setPriority(10);//MAX_PRIORITY

        subThread.start();//启动线程并执行相应的run()方法

        Thread.currentThread().setName("====主线程");//currentThread():静态的,调用当前线程

        for (int i = 0; i <50 ; i++) {
            System.out.println(Thread.currentThread().getName()+Thread.currentThread().getPriority()+i);
//            if(i / 10 == 0) {
//                subThread.yield();
//            }
            if (i == 20){
                try {
                    subThread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

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

你可能感兴趣的:(线程Thread-----方法(1)-----继承:)