当new 一个Thread的时候,就是在主线程的基础上再开一个子线程,cpu一会儿给主线程用,一会儿给子线程用,所以多线程会降低工作效率
一:Thread 与Runnable的使用
1:Thread 自己实现自己的run方法
public static void main(String[] args) throws InterruptedException { new Thread() { @Override public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("thread :" + Thread.currentThread().getName()); } } }.start(); }
2:Thread 与Runnabe结合
public static void main(String[] args) throws InterruptedException { new Thread( new Runnable(){ @Override public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("runnable :" + Thread.currentThread().getName