public class Test implements Runnable { private String name; public Test(String name) { this.name = name; } public void run() { for (int i = 0; i < 100; i++) { System.out.println("线程开始:" + this.name + ",i=" + i); try { Thread.sleep((long) (Math.random() * 600)); // 睡眠时间随机 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { Test t1 = new Test("线程1"); Test t2 = new Test("线程2"); Test t3 = new Test("线程3"); // 实现Runnable接口的子类不像继承Thread类的子类那样有start()方法,可以通过Thread类来启动Runnable线程 new Thread(t1).start(); new Thread(t2).start(); new Thread(t3).start(); } }