1.创建线程的两种方式
package com.gyj.student;
/**
-
多线程
-
@author guoyajie
*/
public class Test1 {public static void main(String[] args){
//======================================
Thread thread = new Thread(){@Override public void run(){ while(true){ try{ Thread.sleep(5000); }catch(Exception e){ e.printStackTrace(); } System.out.println("当前线程名:" + Thread.currentThread().getName()); System.out.println("当前this线程名:" + this.getName()); } } }; thread.start(); //=========================================== Thread thread2 = new Thread(new Runnable() { @Override public void run() { while(true){ try{ Thread.sleep(5000); }catch(Exception e){ e.printStackTrace(); } System.out.println("Runnable当前线程名:" + Thread.currentThread().getName()); //System.out.println("当前this线程名:" + this.getName()); } } }); thread2.start(); //区别runable是Tread的参数 //========================================= new Thread(new Runnable() { @Override public void run() { while(true){ try{ Thread.sleep(5000); }catch(Exception e){ e.printStackTrace(); } System.out.println("Runnable当前线程名:" + Thread.currentThread().getName()); //System.out.println("当前this线程名:" + this.getName()); } }; }){ public void run() { while(true){ try{ Thread.sleep(5000); }catch(Exception e){ e.printStackTrace(); } //子类先运行 System.out.println("Thread当前线程名:" + Thread.currentThread().getName()); } }; }.start();
}
}
多线程不会提高运行效率,性能更低