Thread the template pattern

We know that, in actual use threads when the real execution logic is written on the inside run method, is to run the thread execution unit, if we directly use the Thread class multi-threaded, then the run method itself is an empty implementation, as follows:

/**
     * 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();
        }
    }

  the thread start to achieve the following:

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 */
            }
        }
    }

  

Recalling previous "Template Method," difficult to see, thread start of the run and is a typical profile mode algorithm parent structure referred to achieve the specific details referred to logic subclasses implement.

In accordance with the following thread and start the run way to implement a simple little demo, help you understand.

code show as below:

com.concurrency.designpattern.behavioral.templatemethod Package; 

/ ** 
 * <P> the Title: TemeplateMethod </ P> 
 * <P> the Description: Referring to the thread run and start method, to write a small Demo </ P> 
 * < the p-> Company: http://www.yinjiedu.com </ the p-> 
 * <the p-> Project: Annotation </ the p-> 
 * 
 * @author: weiqi 
 * @date: 2019-12-17 0:45 
 * @Version: 1.0 
 * / 
public class TemeplateMethod { 

    / ** 
     * @description: output character string information input by the user, similar to the thread Start () 
     * @auther: weiqi 
     * @date: 2019-12-17 0:56 
     * @param inputString user input string 
     * / 
    public void outputString Final (string inputString) { 
        System.out.println ( "*******************"); 
        userInput (inputString);
        System.out.println ( "*******************"); 
        System.out.println (); 
    } 

    / ** 
     * @description: exposed to class inheritance the method, similar to the thread RUN () 
     * @auther: weiqi 
     * @date: 2019-12-17 0:57 
     * @param the inputString user input string 
     * / 
    protected void userInput (string the inputString) { 
    } 

    / ** 
     * @description: write simple test function modules 
     * @auther: weiqi 
     * @date: 2019-12-17 0:58 
     * / 
    public static void main (String [] args) { 
        // object reference 1, similar to creating a thread 
        = new new TemeplateMethod temeplateMethod1 TemeplateMethod () { 
            @Override
            void userInput protected (String the inputString) { 
                System.out.println (the inputString); 
            } 
        }; 
        // call algorithm first reference packaging method similar thread calls start () method 
        temeplateMethod1.outputString ( "user1 input data") ; 

        // object reference 2, similar to creating another thread 
        TemeplateMethod temeplateMethod2 new new TemeplateMethod = () { 
            @Override 
            protected void userInput (String the inputString) { 
                System.out.println (the inputString); 
            } 
        }; 
        // call to the second reference algorithm packaging method similar thread calls start () method 
        temeplateMethod2.outputString ( "user2 INPUT Data"); 
    } 
}

  operation result:

When start method stated, adding the synchronized keyword to ensure the atomicity, to check the state before starting the thread, the thread group to join, call the native method algorithm so as a template, completed by the parent Thread.

 Want to know the real-time blog, you can focus the public number "Art of Programming"

Guess you like

Origin www.cnblogs.com/winqi/p/12052331.html