码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-03-28 17:18:15      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:线程优先级

线程优先级

/*
 * 线程优先级,范围[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学习笔记(三十九)- 线程优先级

标签:线程优先级

原文地址:http://blog.csdn.net/wangzi11322/article/details/44702879

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!