JAVA学习笔记(三十九)- 线程优先级

线程优先级

/*
 * 线程优先级,范围[1,10]
 * 不同优先级的线程获取执行的机会不同,优先级越高,执行机会越大
 * 
 * 对比不同优先级的线程被执行的机率
 */
public class Test05 {
    public static void main(String[] args) {
        System.out.println("最大优先级:" + Thread.MAX_PRIORITY);
        System.out.println("最小优先级:" + Thread.MIN_PRIORITY);
        System.out.println("中等优先级:" + Thread.NORM_PRIORITY);
        System.out.println("主线程默认优先级:" + Thread.currentThread().getPriority());
        System.out.println("***********************\n");

        MyThread3 t1=new MyThread3();
        t1.setPriority(10);
        t1.start();
        MyThread4 t2=new MyThread4(3);
        t2.start();

        while (true) {
            System.out.println(Thread.currentThread().getName() + ",优先级:"
                    + Thread.currentThread().getPriority());
        }
    }
}

class MyThread3 extends Thread {

    @Override
    public void run() {
        while (true) {
            System.out.println(Thread.currentThread().getName() + ",优先级:"
                    + getPriority());
        }
    }
}

class MyThread4 extends Thread {
    public MyThread4(int priority) {
        setPriority(priority);
    }

    @Override
    public void run() {
        while (true) {
            System.out.println(Thread.currentThread().getName() + ",优先级:"
                    + getPriority());
        }
    }
}

sleep()和yield()

import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * sleep()和yield()
 */
public class Test06 {
    public static void main(String[] args) {
        /*MyThread5 mt = new MyThread5(250);
        Thread thread = new Thread(mt, "first");
        thread.start();*/

        MyThread6 mt = new MyThread6();
        Thread thread = new Thread(mt, "second");
        thread.start();

        while(true){
            Thread.yield();
            System.out.println(Thread.currentThread().getName()+"****");
        }
    }
}

class MyThread5 implements Runnable {
    private int num;

    public MyThread5(int num) {
        this.num = num;
    }

    @Override
    public void run() {
        while (num > 0) {
            try {
                Thread.sleep(8000);//sleep使线程从执行状态进入休眠状态
                System.out.println(Thread.currentThread().getName() + "****"
                        + num--);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class MyThread6 implements  Runnable{

    @Override
    public void run() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        while(true){
            //yield方法使线程从执行状态进入准备就绪状态
            Thread.yield();
            System.out.println(Thread.currentThread().getName()+",当前时间:"+sdf.format(new Date()));
        }
    }


}

join()方法

/*
 * join()方法
 */
public class Test07 {
    public static void main(String[] args) {
        MyThread7 mt = new MyThread7();
        Thread t1 = new Thread(mt, "first");
        Thread t2 = new Thread(mt, "second");
        t1.start();
        t2.start();

        //当线程二执行完以后再执行主线程
        try {
            System.out.println("t2 is alive?"+t2.isAlive());
            t2.join();
            System.out.println("t2 is alive?"+t2.isAlive());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 1; i <= 100; i++) {
            System.out.println(Thread.currentThread().getName() + "****" + i);
        }
    }
}

class MyThread7 implements Runnable {

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            System.out.println(Thread.currentThread().getName() + "****" + i);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值