如何改变JAVA线程池里面任务的优先级

#第一次写博客
#JAVA线程池
很简单的一个小技巧,就是创建类时,给类增加一个有参的构造方法,并且在创建任务的时候将线程的优先级以参数的存入RUN方法中,以此来实现任务优先级的改变。

在这里插入图片描述

public class test{
        static Lock lock = new ReentrantLock();
        static Integer tict = 180000000;

        public static void main(String[] args) {
            ExecutorService service = Executors.newFixedThreadPool(3);
            service.submit(new One(3, "A窗口"));
            service.submit(new One(2, "B窗口"));
            service.submit(new One(2, "C窗口"));
            service.shutdown();
            while (true){
                if (service.isTerminated()) {
                    break;
                }
            }
            //输出每个线程执行的次数
            for (int i : threadCounter) {
                System.out.println(i);
            }
        }
        static int [] threadCounter = new int[3];
        static class One implements Runnable {
            private final int priority;
            private final String name;

            public One(int priority, String name) {
                this.priority = priority;
                this.name = name;
            }

            @Override
            public void run() {
                Thread.currentThread().setPriority(priority);
                while (true) {
                    try {
                        test.lock.lock();
                        if (test.tict == 0) {
                            break;
                        }

                        if ("A窗口".equals(name)){
                            threadCounter[0]++;
                        }else if ("B窗口".equals(name)){
                            threadCounter[1]++;
                        }else {
                            threadCounter[2]++;
                        }
                        test.tict--;
                        //System.out.println(name + "\t时间: " + (new SimpleDateFormat()).format(System.currentTimeMillis()) + "\t剩余票数:" + SynchronousRequestAop.tict);
                    } finally {
                        test.lock.unlock();
                    }
                }
            }
        }
    }

--------------------------------------------分割线---------------------------------------------------------

下面是一些java相关的书籍,大家有兴趣的可以去下载看一下
以下书籍的提取码皆为:123456
阿里的《java开发手册》==>各种各样的开发规范,提升代码B格用的-_-
https://306t.com/file/28095605-451122435
阿里的《不止代码》==>让你摆脱crud成为真正的技术大牛(其实就是指明了成为技术大牛的方向,有兴趣的可以看一下)
https://306t.com/file/28095605-451122477
《深入理解 Spring Cloud与微服务构建(方志朋)》高清中文版PDF(这本我还没开始看,不过是方志明老师写的可以收藏一下)
https://306t.com/file/28095605-451122529
《Java+8函数式编程》这本书强烈推荐(函数式编程和普通编程的对比,大概就是三维对二维。等你学会了可以对别人降维是打击)
https://306t.com/file/28095605-451122547
《深入理解Java虚拟机JVM高级特性与最佳实践(周志明)》(想深入了解虚拟机的同学可以收藏了)
https://306t.com/file/28095605-451122608
《 Spring Cloud与Docker微服务架构实战-完整版》
https://306t.com/file/28095605-451122683

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值