《JVM 生态编程语言实战》来源于被一位容器云专家问道如何实现一个线程池时, 让我顿感以前研究的 Java 并发控制相关的理论以及多线程并发设计模式忘得九霄云外, 鉴于此, 气愤难平, 决定提升编程技术.
1 Scala 隐式转换函数深入实践
1.1 隐式转换
要实现隐式转换, 只要程序可见的范围内定义隐式转换函数即可. Scala 会自动使用隐式转换函数. 隐式转换函数与普通函数唯一的语法区别就是, 要以 implicit 开头, 而且最好要定义函数返回类型.
案例: 特殊售票窗口(只接受特殊人群, 比如学生, 老人等)
详解: 当调用 buySpecialTicket 函数时, 传入参数 Student 不对照时, 会进行隐式转换为 SpecialPerson
- class SpecialPerson(val name: String)
- class Student(val name: String)
- class Older(val name: String)
- implicit def object2SpecialPerson (obj: Object): SpecialPerson = {
- if (obj.getClass == classOf[Student]) { val stu = obj.asInstanceOf[Student]; new SpecialPerson(stu.name) }
- else if (obj.getClass == classOf[Older]) { val older = obj.asInstanceOf[Older]; new SpecialPerson(older.name) }
- else Nil
- }
- var ticketNumber = 0
- def buySpecialTicket(p: SpecialPerson) = {
- ticketNumber += 1
- "T-" + ticketNumber
- }
- scala> var stu =new Student("qinkaixin")
- stu: Student = Student@2f6bbeb0
- scala> buySpecialTicket(stu)
- res0: String = T-1
1.2 使用隐式转换加强现有类型
隐式转换非常强大的一个功能, 就是可以在不知不觉中加强现有类型的功能. 也就是说, 可以为某个类定义一个加强版的类, 并定义互相之间的隐式转换, 从而让源类在使用加强版的方法时, 由 Scala 自动进行隐式转换为加强类, 然后再调用该方法.
案例: 超人变身
详解: 当调用不属于 Man 的方法时, 指直接调用隐式转换函数 man2superman, 并调用 emitLaser 方法
- class Man(val name: String)
- class Superman(val name: String) {
- def emitLaser = println("emit a laster!")
- }
- implicit def man2superman(man: Man): Superman = new Superman(man.name)
- val leo = new Man("leo")
- leo.emitLaser
1.3 隐式参数
所谓的隐式参数, 指的是在函数或者方法中, 定义一个用 implicit 修饰的参数, 此时 Scala 会尝试找到一个指定类型的, 用 implicit 修饰的对象, 即隐式值, 并注入参数.
Scala 会在两个范围内查找:
一种是当前作用域内可见的 val 或 var 定义的隐式变量;
一种是隐式参数类型的伴生对象内的隐式值
案例: 考试签到
详解: 签到笔作为隐式参数, 注入到函数中, 从而可以使用隐式参数对象的属性.
- class SignPen {
- def write(content: String) = println(content)
- }
- implicit val signPen = new SignPen
- def signForExam(name: String) (implicit signPen: SignPen) {
- signPen.write(name + "come to exam in time.")
- }
2 Java 手写线程池
2.1 PThread 实现
- public class PThread extends Thread
- {
- private ThreadPool pool;
- private Runnable target;
- private boolean isShutDown = false;
- private boolean isIdle = false;
- public PThread(Runnable target, String name, ThreadPool pool)
- {
- super(name);
- this.pool = pool;
- this.target = target;
- }
- public Runnable getTarget()
- {
- return target;
- }
- public boolean isIdle()
- {
- return isIdle;
- }
- public void run()
- {
- while (!isShutDown)
- {
- isIdle = false;
- if (target != null)
- {
- target.run();
- }
- isIdle = true;
- try
- {
- pool.repool(this);
- synchronized (this)
- {
- wait();
- }
- }
- catch (InterruptedException IE)
- {
- }
- isIdle = false;
- }
- }
- public synchronized void setTarget(java.lang.Runnable newTarget)
- {
- target = newTarget;
- notifyAll();
- }
- public synchronized void shutDown()
- {
- isShutDown = true;
- notifyAll();
- }
- }
2.2 ThreadPool 实现
- import java.util.List;
- import java.util.Vector;
- public class ThreadPool
- {
- private static ThreadPool instance = null;
- private List<PThread> idleThreads;
- private int threadCounter;
- private boolean isShutDown = false;
- private ThreadPool()
- {
- this.idleThreads = new Vector(5);
- threadCounter = 0;
- }
- public int getCreatedThreadsCount() {
- return threadCounter;
- }
- public synchronized static ThreadPool getInstance() {
- if (instance == null)
- instance = new ThreadPool();
- return instance;
- }
- protected synchronized void repool(PThread repoolingThread)
- {
- if (!isShutDown)
- {
- idleThreads.add(repoolingThread);
- }
- else
- {
- repoolingThread.shutDown();
- }
- }
- public synchronized void shutdown()
- {
- isShutDown = true;
- for (int threadIndex = 0; threadIndex <idleThreads.size(); threadIndex++)
- {
- PThread idleThread = (PThread) idleThreads.get(threadIndex);
- idleThread.shutDown();
- }
- }
- public synchronized void start(Runnable target)
- {
- PThread thread = null;
- if (idleThreads.size()> 0)
- {
- int lastIndex = idleThreads.size() - 1;
- thread = (PThread) idleThreads.get(lastIndex);
- idleThreads.remove(lastIndex);
- thread.setTarget(target);
- }
- else
- {
- threadCounter++;
- thread = new PThread(target, "PThread #" + threadCounter, this);
- thread.start();
- }
- }
- }
2.3 ThreadPool 测试
- public void testThreadPool() throws InterruptedException {
- long starttime=System.currentTimeMillis();
- for(int i=0;i<1000;i++){
- ThreadPool.getInstance().start(new MyThread("testThreadPool"+Integer.toString(i)));
- }
- long endtime=System.currentTimeMillis();
- System.out.println("testThreadPool"+":"+(endtime-starttime));
- System.out.println("getCreatedThreadsCount:"+ThreadPool.getInstance().getCreatedThreadsCount());
- Thread.sleep(1000);
- }
3 未完待续
来源: https://juejin.im/post/5c376fc351882525dc62ceaa