线程的两种创建方法
方法一:
public class testthread1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Runner1 r = new Runner1(); r.start(); for(int i = 0;i<100;i++) { System.out.println("main thread:......."+i); } } } class Runner1 extends Thread { public void run() { for(int i = 0;i<100;i++) { System.out.println("runner1:"+i); } } }
方法二:
public class testthread2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Runner2 r = new Runner2();//如果直接r。run的话,相当与调用方法。不存在线程的问题。递归调用 Thread t = new Thread(r);//thread有这样的构造方法 t.start(); for(int i = 0;i<100;i++) { System.out.println("main thread:......."+i); } } } class Runner2 implements Runnable { public void run() { for(int i = 0;i<100;i++) { System.out.println("runner1:"+i); } } }