我们知道,在实际使用线程的时候,真正的执行逻辑都是写在run方法里面,run方法是线程的执行单元,如果我们直接使用Thread类实现多线程,那么run方法本身就是一个空的实现,如下:

/**
* If this thread was constructed using a separate
* <code>Runnable</code> run object, then that
* <code>Runnable</code> object's <code>run</code> method is called;
* otherwise, this method does nothing and returns.
* <p>
* Subclasses of <code>Thread</code> should override this method.
*
* @see #start()
* @see #stop()
* @see #Thread(ThreadGroup, Runnable, String)
*/
@Override
public void run() {
if (target != null) {
target.run();
}
}

  thread中start实现如下:

public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException(); /* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this); boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}

  

回顾之前的《模板方法模式》不难看出,thread中的run和start是一个比较典型的模板模式,算法结构交由父类实现,具体的逻辑细节交由子类实现。

下面按照thread的run和start方式来实现一个简单的小demo,帮助大家理解。

代码如下:

package com.concurrency.designpattern.behavioral.templatemethod;

/**
* <p>Title: TemeplateMethod</p>
* <p>Description: 参照thread的run和start方法,写的一个小demo</p>
* <p>Company: http://www.yinjiedu.com</p>
* <p>Project: annotation</p>
*
* @author: WEIQI
* @Date: 2019-12-17 0:45
* @Version: 1.0
*/
public class TemeplateMethod { /**
* @description: 输出用户输入的字符串信息,类似于thread中的start()
* @auther: WEIQI
* @date: 2019-12-17 0:56
* @param inputString 用户输入字符串
*/
public final void outputString(String inputString) {
System.out.println("*******************");
userInput(inputString);
System.out.println("*******************");
System.out.println();
} /**
* @description: 暴露给继承类的方法,类似于thread中的run()
* @auther: WEIQI
* @date: 2019-12-17 0:57
* @param inputString 用户输入字符串
*/
protected void userInput(String inputString) {
} /**
* @description: 编写简单的测试功能模块
* @auther: WEIQI
* @date: 2019-12-17 0:58
*/
public static void main(String[] args) {
// 对象引用1,类似于创建一个线程
TemeplateMethod temeplateMethod1 = new TemeplateMethod(){
@Override
protected void userInput(String inputString) {
System.out.println(inputString);
}
};
// 第一个引用调用算法封装方法,类似于线程调用start()方法
temeplateMethod1.outputString("user1 input data"); // 对象引用2,类似于创建另外一个线程
TemeplateMethod temeplateMethod2 = new TemeplateMethod(){
@Override
protected void userInput(String inputString) {
System.out.println(inputString);
}
};
// 第二个引用调用算法封装方法,类似于线程调用start()方法
temeplateMethod2.outputString("user2 input data");
}
}

  运行结果:

在start方法申明时,加了synchronized 关键字保证了原子性,对于启动线程之前状态检查、加入线程组、调用native方法等都作为算法模板,由父类Thread完成。

想要了解实时博文,可以关注公众号《编程之艺术》

最新文章

  1. 进阶篇:以IL为剑,直指async/await
  2. 减小Delphi2010程序的尺寸(关闭RTTI反射机制)
  3. 又一种Mysql报错注入
  4. 用diss 实现 push动画
  5. JavaScript获取后台C#变量以及调用后台方法
  6. MySQL和MongoDB语句的写法对照
  7. jQuery 单选按钮切换
  8. 【模拟】BAPC2014 G Growling Gears (Codeforces GYM 100526)
  9. QListWidget与QTableWidget的使用以及样式设置
  10. CSS---input标签注意
  11. Oracle SQL语句执行过程
  12. zookeeper curator处理会话过期session expired
  13. swiper插件的简单使用,实现图片轮播
  14. 正确启动从GitHub上下载的vue项目:vueAdmin-template
  15. Metasploit Framework(5)弱点扫描
  16. Java当出现未被捕获的异常应该如何处理
  17. Spark 核心篇-SparkContext
  18. MongoDB中_class字段的作用
  19. adb无线网络调试
  20. Java 集合-集合介绍

热门文章

  1. 分析Jackson的安全漏洞CVE-2019-12086
  2. 目前为止最简洁的C#文件夹Copy代码,不接受反驳
  3. 高通平台:USB充电【转】
  4. Python对MySql增删改查
  5. 《Linux/UNIX系统编程手册》第56章 SOCKET:介绍
  6. 使用EA将源码转化为类图
  7. kettle教程---通过配置表格配置实现数据的批量增量更新(实用)
  8. 手工挖掘web常见漏洞时的一些经验总结
  9. R的环境系统
  10. linux编程fcntl获取和设置文件状态