package org.hanqi.thread;public class Test1 { public static void main(String[] args) { // for(int i=0;i<10;i++)// {// System.out.println(i);// // try {// //线程的休眠方法(毫秒)// Thread.sleep(1000);// } catch (InterruptedException e) {// // e.printStackTrace();// }// } TestThread1 tt1=new TestThread1(); //启动多线程 tt1.start(); TestThread1 tt2=new TestThread1(); tt2.start(); //启动实现接口方式的多线程 Thread t3=new Thread(new TestThread2()); t3.start(); }}
package org.hanqi.thread;//支持多线程//1.继承Thread//2.覆盖run方法public class TestThread1 extends Thread { //重写 public void run() { for(int i=0;i<10;i++) { System.out.println(i); try { //线程的休眠方法(毫秒) Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
package org.hanqi.thread;public class TestThread2 implements Runnable { @Override public void run() { for(int i=0;i<10;i++) { System.out.println(i); try { //线程的休眠方法(毫秒) Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }}