我是靠谱客的博主 靓丽唇膏,最近开发中收集的这篇文章主要介绍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);
        }
    }
}

最后

以上就是靓丽唇膏为你收集整理的JAVA学习笔记(三十九)- 线程优先级的全部内容,希望文章能够帮你解决JAVA学习笔记(三十九)- 线程优先级所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部