两种线程创建方式的比较 发表于 2018-02-22 | 分类于 juc 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273package cn.itcast.heima2;public class TraditionalThread { /** * @param args */ public static void main(String[] args) { Thread thread = new Thread(){//todo Thread子类 @Override public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("1:" + Thread.currentThread().getName()); System.out.println("2:" + this.getName()); } } }; thread.start(); Thread thread2 = new Thread(new Runnable(){//todo 传入Runnable @Override public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("1:" + Thread.currentThread().getName()); } } }); thread2.start(); new Thread(//todo 会使用Thread子类的方式执行 new Runnable(){ public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("runnable :" + Thread.currentThread().getName());//不会被执行 } } } ){ public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("thread :" + Thread.currentThread().getName()); } } }.start(); }} 多线程一定会提高程序效率吗?https://blog.csdn.net/tigerjin/article/details/74142304