Thread 中用到的两种设计模式

模板模式

模板模式的应用就比较好理解了。在创建线程一般使用构建 Thread 类或者实现 Runnable 接口(这种说法是错误的,最起码是不严谨的,在 JDK 中代表线程的就只有 Thread 这个类,线程的执行单元就是 run() 方法,你可以通过继承 Thread 然后重写 run() 方法实现自己的业务逻辑,也可以实现 Runnable 接口实现自己的业务逻辑),启动线程是使用的 start() 方法,但是具体业务逻辑还是在 run() 方法中。

/**

    * Causes this thread to begin execution; the Java Virtual Machine

    * calls the <code>run</code> method of this thread.

    * <p>

    * The result is that two threads are running concurrently: the

    * current thread (which returns from the call to the

    * <code>start</code> method) and the other thread (which executes its

    * <code>run</code> method).

    * <p>

    * It is never legal to start a thread more than once.

    * In particular, a thread may not be restarted once it has completed

    * execution.

    *

    * @exception  IllegalThreadStateException  if the thread was already

    *              started.

    * @see        #run()

    * @see        #stop()

    */

    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的应用中看到了一个挺好的例子:

/**

* Description

* <p>

* </p>

* DATE 2018/10/26.

*

* @author caichengzhang.

*/

public class Template {

    public Template() {

    }

    public final void start(){

        System.out.println("stat方法启动!");

        run();

        System.out.println("start方法结束!");

    }

    public void run(){}

    public static void main(String[] args) {

        Template template = new Template(){

            @Override

            public void run() {

                System.out.println("run方法开始运行!");

            }

        };

        template.start();

    }

}

策略模式

结合上面的 Thread 的 start() 方法,注释说明 start() 方法执行的是它的 run() 方法,再看看 Thread 的 run() 方法,如果构造传入 Runnable,就执行 Runnable 的 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();

        }

    }

/* What will be run. */

privateRunnable target;

无论是 Runnable 的 run() 方法,还是 Thread 类本身的 run() 方法(事实上 Thread 类也是实现了 Runnable 接口)都是想将线程的控制本身和业务逻辑的运行分离开来,达到职责分明、功能单一的原则,这一点与 GoF 设计模式中的策略设计模式很相似。

这个 Runnable 就是策略接口,针对不同的策略实现,执行相应的方法。这样对策略进行不同的实现即可。

package com.example.threaddesign;

/**

* @author Dongguabai

* @date 2018/12/2 20:58

*/

public class ThreadTest {

    public static void main(String[] args) {

        Thread thread = new Thread(new Strategy1(), "执行者");

        thread.start();

    }

    static class Strategy1 implements Runnable {

        @Override

        public void run() {

            System.out.println("策略一");

        }

    }

    static class Strategy3 implements Runnable {

        @Override

        public void run() {

            System.out.println("策略三");

        }

    }

    static class Strategy2 implements Runnable {

        @Override

        public void run() {

            System.out.println("策略二");

        }

    }

}

感兴趣可以加Java架构师群获取Java工程化、高性能及分布式、高性能、深入浅出。高架构。性能调优、Spring,MyBatis,Netty源码分析和大数据等多个知识点高级进阶干货的直播免费学习权限 都是大牛带飞 让你少走很多的弯路的 群..号是:855801563 对了 小白勿进 最好是有开发经验

注:加群要求

1、具有工作经验的,面对目前流行的技术不知从何下手,需要突破技术瓶颈的可以加。

2、在公司待久了,过得很安逸,但跳槽时面试碰壁。需要在短时间内进修、跳槽拿高薪的可以加。

3、如果没有工作经验,但基础非常扎实,对java工作机制,常用设计思想,常用java开发框架掌握熟练的,可以加。

4、觉得自己很牛B,一般需求都能搞定。但是所学的知识点没有系统化,很难在技术领域继续突破的可以加。

5.阿里Java高级大牛直播讲解知识点,分享知识,多年工作经验的梳理和总结,带着大家全面、科学地建立自己的技术体系和技术认知!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,785评论 18 399
  • 进程和线程 进程 所有运行中的任务通常对应一个进程,当一个程序进入内存运行时,即变成一个进程.进程是处于运行过程中...
    胜浩_ae28阅读 5,159评论 0 23
  • 空中有一座城 这座城如此遥远 使人苦恼于他荒谬的存在 突然,无限的空间变为化石 古老的圆月用魔法控制的 世界如此美...
    Ailx阅读 206评论 0 0
  • 2-4cm❌ 5cm日常 7cm小礼服 10cm女王 剩下我就不想了
    Jasmine_csy阅读 234评论 0 0