秦凯新技术社区推出的《Coding技术进阶实战》系列即将上线,包含语言类精深的用法和技巧,涵盖 python,Java,Scala,Tensorflow等主流大数据和深度学习技术基础,敬请期待。为何我会写这样一个系列,来源于被一位容器云专家问到如何实现一个线程池时,让我顿感之前研究的Java并发控制相关的理论以及多线程并发设计模式忘得九霄云外,鉴于此,气愤难平,决定展现我的编程魅力。java
版权声明:本套技术专栏是做者(秦凯新)平时工做的总结和升华,经过从真实商业环境抽取案例进行总结和分享,并给出商业应用的调优建议和集群环境容量规划等内容,请持续关注本套博客。QQ邮箱地址:1120746959@qq.com,若有任何技术交流,可随时联系。python
要实现隐式转换,只要程序可见的范围内定义隐式转换函数便可。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
复制代码
隐式转换很是强大的一个功能,就是能够在不知不觉中增强现有类型的功能。也就是说,能够为某个类定义一个增强版的类,并定义互相之间的隐式转换,从而让源类在使用增强版的方法时,由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
复制代码
所谓的隐式参数,指的是在函数或者方法中,定义一个用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.")
}
复制代码
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();
}
}
复制代码
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();
}
}
}
复制代码
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);
}
复制代码