Java—多线程优先级,守护线程

线程优先级

线程的优先级指的是,线程的优先级越高越有可能先执行,但仅仅是有可能而已。

/**
*设置优先级
**/
public final void setPriority(int newPriority)

/**
*取得优先级
**/
public final int getPriority()

对于优先级设置的内容可以通过Thread类的几个常量来决定:

  1. 最高优先级:public final static int MAX_PRIORITY = 10;
  2. 中等优先级:public final static int NORM_PRIORITY = 5;
  3. 最低优先级:public final static int MIN_PRIORITY = 1;
/**设置优先级,获取主线程的优先级**/

class MyThread1 implements Runnable{

    @Override
    public void run(){
        Thread t = Thread.currentThread();//创建一个当前线程的实例化对象
        System.out.println("当前线程"+t.getName()+"的优先级是:"+t.getPriority());
    }
}

public class SetPriority {

    public static void main(String[] args) {

        /*获取主方法的线程优先级*/
        Thread threadA = new Thread(() -> {
            System.out.println(Thread.currentThread().getName()
                    + "的优先级是:" + Thread.currentThread().getPriority());

        }, "主线程main");
        threadA.start();
        //由结果得知,主方法的线程也只是一个中等优先级5

        /*将线程优先级设置为6*/
        //threadA.setPriority(6);将线程优先级设置为6
        //threadA.start();

        /*设置子线程的优先级*/
        MyThread1 mt = new MyThread1();
        Thread t1 = new Thread(mt,"子线程A");
        Thread t2 = new Thread(mt,"子线程B");
        Thread t3 = new Thread(mt,"子线程C");
        t1.setPriority(MIN_PRIORITY);
        t2.setPriority(NORM_PRIORITY);
        t3.setPriority(MAX_PRIORITY);
        t1.start();
        t2.start();
        t3.start();
    }
}
线程具有继承性

线程继承性主要体现在:如果此时在线程A中启动一个线程B,那么B和A的优先级将是一样的

/**线程的继承性**/
class MyThread2 implements Runnable{
    @Override
    public void run(){
        System.out.println("线程A的优先级为:"+Thread.currentThread().getPriority());
        Thread threadA = new Thread(new MyThread3());//实例化线程B对象
        threadA.start();
    }
}

class MyThread3 implements Runnable{
    @Override
    public void run(){
        System.out.println("线程B的优先级为:"+Thread.currentThread().getPriority());
    }
}

public class ImplThread {
    public static void main(String[] args) {
        Thread thread = new Thread(new MyThread2());
        thread.setPriority(Thread.MAX_PRIORITY);
        thread.start();
    }
}
守护线程

Java中有两种线程:用户线程和守护线程

用户线程和守护线程的区别:通过调用isDaemon()方法辨别

返回false的为"用户线程",否则为"守护线程"

典型的守护线程是垃圾回收线程。只要当前JVM进程中存在任何一个还未结束的非守护线程,守护线程就会一直工作,只有当最后一个非守护线程结束时,守护线程才会随着JVM一起停止工作。

注意:主线程main为用户线程

/**守护线程**/
class DaemonRunnable implements Runnable{
    @Override
    public void run(){
        int i = 0;
        try {

            while(true) {
                System.out.println(Thread.currentThread().getName() + " 调用第 " + ++i + " 次");
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            System.out.println(Thread.currentThread().getName() + " 中断了");
        }
    }
}

public class DaemonThread {

    public static void main(String[] args) {

        /**测试主线程为何种线程类型**/
        System.out.println(Thread.currentThread().isDaemon());
        //输出:false,是用户线程

        Thread threadA = new Thread(new DaemonRunnable(),"子线程A");
        threadA.setDaemon(true);//垃圾回收线程,此方法调用要在启动线程之前
        threadA.start();

        Thread threadB = new Thread(new DaemonRunnable(),"子线程B");//用户线程
        threadB.start();

        try {
            Thread.sleep(3000);
            threadB.interrupt();
            Thread.sleep(1000);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

由上述代码不难看出,用户线程B中断后守护线程没有立即结束,而是等主线程(用户线程)结束后才结束。

完整代码:https://github.com/Loinbo/Java/tree/master/MulThread2

你可能感兴趣的:(java)