标签:color extends star system div str tar 运行 imp
创建线程有2种方式,继承Thread类 和 实现Runnable接口的方式
public class ThreadTest { public static void main(String[] args) { System.out.println("main-start..."); MyThread t = new MyThread(); t.start(); // 启动新线程t1 Thread t2 = new Thread(new MyRunnable()); t2.start(); // 启动新线程t2 System.out.println("main-end..."); } } /** * 继承 Thread 方式 */ class MyThread extends Thread { @Override public void run() { System.out.println("by Thread start new thread!"); } } /** * 实现Runnable接口方式 */ class MyRunnable implements Runnable { @Override public void run() { System.out.println("by Runnable start new thread!"); } }
运行多次控制台输出:
main-start...
by Thread start new thread!
main-end...
by Runnable start new thread!
或者
main-start...
by Thread start new thread!
by Runnable start new thread!
main-end...
结果会有不同
标签:color extends star system div str tar 运行 imp
原文地址:https://www.cnblogs.com/wanjun-top/p/12817965.html