其实有时候不能简单说哪种设计用到了哪些设计模式,设计模式本身就是对很多代码设计经验的总结。
/** * 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 */ } } }
/** * 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(); } }
/** * 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. */ private Runnable target;
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("策略二"); } } }
知了堂为大学生/初学者提供编程实践学习指导和服务,通过前沿项目实战、自有学习/实践平台、专业学习体系、形式多样的线上线下活动,让学习者真正掌握编程技能、具备编程实战经验,获得入职/职场晋升能力,同时知了堂着力打造一个有温度的学习社区,通过链接学习导师、举行社区活动、规范社区交流机制,为学习者提供交流、分享、项目组队等空间,共建最有价值的编程学习者成长社区。