/** 方式:Runnable匿名子类+Thread匿名子类: * Thread执行流程是这样的:<br> * 找子类的run方法,若没有就找父类的run方法,父类的run方法回去找Runnable的run方法 */ new 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();
参考Thread的run方法源码
public void run() { if (target != null) { target.run(); } }
时间: 2024-10-19 23:51:57