chap9-4.txt

来自「清华大学出版社经典教材系列」· 文本 代码 · 共 33 行

TXT
33
字号
// 程序9-4
class threadTest extends Thread{// 定义一个thread的子类
    threadTest(String str){
        super(str);		// 调用父类构造函数设置线程的名字
    }
    
    public void run( ){
        try{
            Thread.sleep(2); 	// 睡眠2毫秒,通过睡眠模拟程序的执行
        }catch(InterruptedException e) {
            System.err.println(e.toString( ));
        }

        System.out.println(getName( )+" " +getPriority( )); // 输出线程的名字和优先级
    }
}

public class multTheadOne{	// 定义一个主类
    public static void main(String agrs[]){
        Thread one=new threadTest("one" );	// 定义3个线程对象
        Thread two=new threadTest("two" );
        Thread three=new threadTest("three" );
        
        one.setPriority(Thread.MIN_PRIORITY);	// 设置各个线程的优先级
        two.setPriority(Thread.NORM_PRIORITY);
        three.setPriority(Thread.MAX_PRIORITY);
        
        one.start( );		// 首先启动one线程
        two.start( );
        three.start( );
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?