Two ways to create ways of threads - Thread

Package cn.learn.thread.Thread;
 / * 
    implement Runnable must override the run () method is generally recommended to achieve Runnable 

 * / 
public  class RunnableImpl the implements Runnable { 

    @Override 
    public  void run () { 
        System.out.println (the Thread. . currentThread () getName () + "executed" ); 
    } 
}
Package cn.learn.thread.Thread;
 / * 
     inheritance Thread class, multi-threaded 
     implementation steps: 
        1. Create a subclass of class Thread 
        2. Thread class override the run method of task threads disposed in a subclass of Thread 
        3 Creating a thread subclass object 
        4. call the start method of the thread class, open a new thread run method 
        results are two processes running concurrently, the current thread (main thread) and another thread (which executes its run method) 
        many times it is illegal to start a thread, especially after the end of the thread, you can not restart the 
     java belong preemptive execution, which thread priority level to perform another job; priority with a random selection 

 * / 
public  class ThreadExtends the extends the thread { 

    public ThreadExtends () { 
    } 

    // use the parent class name to create a new thread 
    public ThreadExtends (String name) {
         Super (name); 
    } 

    //Re-run method 
    @Override
     public  void run () {
         // get the thread name
         // String name = this.getName ();
         // System.out.println ( "Thread Name:" + name); 

        // Get the current thread name 
        Thr = the Thread Thread.currentThread (); 
        System.out.println (Thr); 

        // chain programmed
         // System.out.println (Thread.currentThread () getName ().); 
    } 
}
1  Package Penalty for cn.learn.thread.Thread;
 2  / * 
3      the JVM path to perform the main method to find methods leading to the main OS to open up a cpu of
 4      this path called main thread - the main thread
 5      cpu through the thread, you can perform the main path method
 6  
7      the thread Runnable interface inheritance and the difference between the
 8      implements the Runnable interface to create multiple threads of benefits:
 9        1. to avoid the limitations of single inheritance
 10        2. enhance the extension of the program, reducing the coupling (decoupling), eg: Runnable plurality implementation class can have different task threads
 11        3. thread a plurality of objects, a shared resource Runnable implementation class object
 12 is   * / 
13 is  
14  public  class the MyThread {
 15      public  static  void main (String [] args) throws{InterruptedException
 16          // Create Thread class subclass object, has a structure as ThreadExtends methods which may be created in the class with the super (name) the name of the parent thread 
. 17          ThreadExtends anThread = new new ThreadExtends ();
 18 is  
. 19          // can anThread. setName (); to set the thread name
 20  
21          // open up a new thread to execute the run method, if the direct method using a run, or single-threaded, the main methods to perform 
22          anThread.start (); // open new stack space, new thread, ThreadExtends class run method runs, get the name of the thread-0 the thread
 23  
24-          // then create a new thread, run run () to get the thread name-1 the thread 
25          new new ThreadExtends () Start ();.   / / the thread [the thread-1, 5, main]
 26  
27          // get the current thread name
28          System.out.println (Thread.currentThread () getName ().);   // main
 29  
30          // above the concurrently executing threads, insecurity, seize cpu, cpu selective claimed randomly performed 
31 is          for ( int 0 = I; I <10; I ++ ) {
 32              System.out.println ( "main" + I);
 33 is          }
 34 is  
35  
36          // the thread specified number of milliseconds to sleep temporarily stopped thread of execution to continue after the end of
 37          / / analog stopwatch 
38 is          for ( int I = 0; I <60; I ++ ) {
 39              Thread.currentThread () SLEEP (1000. );
 40             System.out.println (I);
 41 is  
42 is          }
 43 is  
44 is  
45          // second method of creating a thread 
46          / * 
47          Another way to create a thread is to declare classes implement Runnable.
48          class and the run method implemented. Instance of that class can then be allocated, and passed as an argument to start when creating Thread.
49  
50          need to accept the Thread object, call start, run run open a new thread
 51 is          * / 
52 is          // . 1 
53 is          RunnableImpl runnableThread = new new RunnableImpl ();
 54 is          // 2, a plurality of Thread shared resource runnableThread 
55          Thread T = new new the Thread (runnableThread);
 56 is         T1 = the Thread new new the Thread (runnableThread);
 57 is          // . 3 
58          t.start ();   // new thread turn 
59          t1.start ();   // new thread turn 
60  
61 is      }
 62 }

 

Guess you like

Origin www.cnblogs.com/huxiaobai/p/11519673.html