自定义类MyThread继承Thread类:
package com.shusheng.tihuzhai.test; /** * @author shusheng * @description * @Email shusheng@yiji.com * @date 2018/8/27 14:54 */ public class MyThread extends Thread { public MyThread(){} public MyThread(String name){ super(name); } @Override public void run(){ for(int x=0;x<2000;x++){ System.out.println(getName()+":"+x); } } }
创建对象,给线程命名,启动线程:
package com.shusheng.tihuzhai.test; /** * @author shusheng * @description * @Email shusheng@yiji.com * @date 2018/8/27 14:59 */ public class TestMyThread { public static void main(String[] args){ MyThread my1 = new MyThread("张三"); MyThread my2 = new MyThread("李四"); System.out.println(Thread.currentThread().getName()); my1.start(); my2.start(); } }