#第一次写博客
#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