线程的各种设置

package chapter1;

public class TestTwo {
    public static void main(String[] args) throws InterruptedException {

        MyThread thread1 = new MyThread("线程1");
        MyThread thread2 = new MyThread("线程2");
//        thread1.setPriority(1);
//        thread2.setPriority(10);
//        thread1.setName("555");
        thread1.start();
        Thread.sleep(2);
        thread1.interrupt();
        thread2.start();
//        thread1.setPriority(1);
//        System.out.println(thread1.getPriority());
//        thread1.start();
//        int j = 0;
//        for (int i = 0; i < 100000; i++) {
//            j+=i;
//        }
//        System.out.println("Main"+System.currentTimeMillis());
    }
}

package chapter1;

public class MyThread extends Thread{
    public MyThread(String name){
        super(name);
    }
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (isInterrupted()){
                System.out.println("The ThreadOne is interrupted.");
                break;
            }
            System.out.println(getName()+": "+i);
        }
    }
}

发布了55 篇原创文章 · 获赞 15 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_43141611/article/details/105627523
今日推荐