线程(9)--线程的优先级和后台线程

线程的优先级:
1. java中对线程的优先级定义了10个级别,范围:1 (Thread.MIN_PRIORITY )~ 10 (Thread.MAX_PRIORITY )),但不同的系统有不同 的线程优先级的取值范围
2. Thread类的默认优先级是5(Thread.NORM_PRIORITY)
3. 设置线程优先级使用setPriority(int)方法
展示线程优先级的一个小案例:
FatherThread.java

package com.poriority.entity;
/**
 * @version 2018-1-2 上午8:43:54
 */

public class FatherThread extends Thread{
    public FatherThread(){
        //设置类的优先级,需要通过构造方法设置类的优先级,此处设置的优先级会影响子类的优先级
        this.setPriority(8);
    }
}

SonThread.java

package com.poriority.entity;
/**
 * @version 2018-1-2 上午8:53:04
 */
public class SonThread extends FatherThread{
    //子类的优先级继承于父类的优先级
}

TestPriority.java

package com.poriority.test;
import com.poriority.entity.FatherThread;
import com.poriority.entity.SonThread;
/**
 * @version 2018-1-2 上午8:45:02
 */
public class TestPriority {
    public static void main(String[] args) {
        Thread thread = new Thread();
        System.out.println("默认线程的优先级是:" + thread.getPriority());

        FatherThread ft = new FatherThread();
        //默认线程的优先级是继承父类的优先级
        System.out.println("FatherThread的线程优先级是:" + ft.getPriority());
        //此处设置的优先级,只用于设置当前对象的优先级,对于子类的优先级没有影响
        ft.setPriority(9);
        System.out.println("FatherThread的线程设置的优先级是:" + ft.getPriority());


        SonThread st = new SonThread();
        //子类默认的优先级是继承父类的优先级
        System.out.println("SonThread的线程优先级是:" + st.getPriority());

    }
}

线程优先级总结:
子类线程的默认优先级是继承直接父类线程的优先级,对象的优先级是通过对象.serPriority(int)方法来实现的,类的优先级是通过构造方法来实现的this.SetPriority(int)。
后台线程:
1. 在系统的后台运行,不与用户直接交互;这样的 线程称为后台线程(也称服务线程或守护线程)。
– 例如:文件下载
2. setDaemon()方法:将线程设置为后台线程
展示后台线程的一个小案例:
FatherThread.java

package com.poriority.entity;

/**
 * @version 2018-1-2 上午8:43:54
 */

public class FatherThread extends Thread{
    public FatherThread(){
        //设置类的优先级,需要通过构造方法设置类的优先级,此处设置的优先级会影响子类的优先级
        this.setPriority(8);
    }
    //构造方法,设置线程的名字
    public FatherThread(String name){
        super(name);
    }

    //线程
    @Override
    public void run() {
        try {
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

SonThread.java

package com.poriority.entity;
/**
 * @version 2018-1-2 上午8:53:04
 */

public class SonThread extends FatherThread{
    //子类的优先级继承于父类的优先级

    //线程
        @Override
        public void run() {
            try {
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
}

TestThread.java

package com.poriority.test;
import com.poriority.entity.FatherThread;
import com.poriority.entity.SonThread;

/**
 * @version 2018-1-2 上午8:45:02
 */

public class TestPriority {

    public static void main(String[] args) {
        Thread thread = new Thread();
        System.out.println("默认线程的优先级是:" + thread.getPriority());

        FatherThread ft = new FatherThread("father");
        //默认线程的优先级是继承父类的优先级
        System.out.println("FatherThread的线程优先级是:" + ft.getPriority());
        //此处设置的优先级,只用于设置当前对象的优先级,对于子类的优先级没有影响
        ft.setPriority(9);
        ft.start();
        System.out.println("FatherThread的线程设置的优先级是:" + ft.getPriority());


        SonThread st = new SonThread();
        //将子类线程设置为后台线程,参数为boolean类型的,true表示设置为后台线程,false表示不设置为后台线程,后台线程的设置必须在线程启动之前设置
        st.setDaemon(true);
        st.start();
        //子类默认的优先级是继承父类的优先级
        System.out.println("SonThread的线程优先级是:" + st.getPriority());

    }

}

后台线程总结
后台线程的设置方法是setDaemon()方法,在线程启动之前进行设置,查看后台线程的方法是单步执行的时候查看,给线程重命名的方法是使用构造方法,在构造方法中调用父类的方法进行重命名。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值