The second way to create threads ------ implement Runnable way

package cn.itcast.demo16.Demo07.Runnable;

/**
* @author newcityman
* @date 2019/7/22 - 23:17
*/
public class RunnableImpl implements Runnable {
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
System.out.println(Thread.currentThread().getName()+"---->"+i);
}
}
}


package cn.itcast.demo16.Demo07.Runnable;

/**
* @author newcityman
* @date 2019/7/22 - 23:17
*/
public class RunnableImpl2 implements Runnable {
@Override
public void run() {
for (int i = 0; i <20 ; i++) {
System.out.println("hello world---->"+i);
}
}
}


cn.itcast.demo16.Demo07.Runnable Package Penalty for ; 

/ **
* @author newcityman
* @date 2019/7/22 - 23:18
* The second way to create multi-threaded: implement Runnable
* java.lang.Runnable
* Runnable interface should be implemented by those who intend to carry out its example by a thread of class. Class must be given a method called run no arguments
constructor class the java.lang.Thread *
*
*
* implemented steps of:
* 1, to create a class that implements Runnable interface
* 2, rewriting Runnable interface implementation class in the run the method provided threaded tasks
* 3, create an object Runnable implementation class
* 4, create a thread class object, the parameters constructor pass an object Runnable implementation class
* 5, calls start () method thread class
*
* achieve Runnable interface to create multiple threads advantage
* 1, to avoid the limitations of single inheritance
* a class can only inherit a class, the class inherits the thread class will not be able to inherit the other classes
* implement Runnable interface, you can also inherit from other classes, implement other interfaces
* 2, to enhance the extension of the program, the program reduces the coupling (decoupled)
manner implement Runnable *, provided it opened threaded tasks and threads isolated (decoupled)
* implementation classes, override the run A method for setting threaded tasks
* Create thread class object, calling the Start method, to open a new thread
* /
public class Demo01Runnable {
public static void main (String [] args) {
RunnableImpl impl = new new RunnableImpl () ;
thread T = the Thread new new (impl) ;
t.start () ;
. the System Out.println ( "=========================") ;
new new the Thread ( . RunnableImpl2 new new ()) Start () ;
for ( int I = 0 ; I < 20 is ; I ++) {
the System. Out.println (the Thread.currentThread().getName()+"---->"+i);
}

}
}
 

Guess you like

Origin www.cnblogs.com/newcityboy/p/11229250.html