java 开始一个现成有两中方法 :一是直接调用Thread实例的start()方法,二是将Runable实例传给一个Thread实例然后调用它的start()方法。
首先让我们看看Thread类的start() 方法:
public synchronized void start() {
if (started)
throw new IllegalThreadStateException();
started = true;
group.add(this);
start0();
}
一个Thread的实例一旦调用了start() 方法,这个实例的started标记就标记为true,不管这个线程是否给执行,只要调用了一次start()就再也没有机会运行了.这意味着:通过Thread的start() 方法,一个Thread只能生成一个线程.所以下面的代码会抛出异常:
public class Test { public static void main(String[] args) throws Exception{ MyThread mt = new MyThread(); mt.start(); mt.join();//在线程对象a上调用join()方法,就是让当前正在执行的线程等待线程对象a对应的线程运行完成后才继续运行 Thread.sleep(3000); mt.start(); } }
当线程对象mt运行完成后,我们让主线程休息一下,然后我们再次在这个线程对象上启动线程。结果我们看到:
Exception in thread "main" java.lang.IllegalThreadStateException
如果要让一个实例产生多个线程,着就要使用Runnable 借口的功能了.
如果我们把一个Runnable实例给Thread对象多次包装,我们就可以看到它们实际是在同一实例上启动线程:
class R implements Runnable{ private int x = 0; public void run(){ for(int i=0;i<100;i++){ try{ Thread.sleep(10); }catch(Exception e){} System.out.println(x++); } } }
public class Test { public static void main(String[] args) throws Exception{ R r = new R(); for(int i=0;i<10;i++) new Thread(r).start(); } }
x 是实例变量,但结果是x被加到了999,说明这10个线程是在同一个r对象上运行的.
但是如果使用Thread 来使用线程:
class MyThread extends Thread{
public int x = 0;
public void run(){
for(int i=0;i<100;i++){
try{
Thread.sleep(10);
}catch(Exception e){}
System.out.println(x++);
}
}
}
public class Test {
public static void main(String[] args) throws Exception{
for(int i=0;i<10;i++)
new MyThread ().start();
}
}
x实例变量被创建了10次,每个现程创建了一次,每个x都是加到了10.不像Runnable 对象,每个现成都共享同一个Runnable 对象,共享同一个实例变x.
我们把上面用Runnable 方式创建的线程,同一实例(Runnable实例)的多个线程。
从dev2dev网友axman的go deep into java专栏文档,结合自己的总结而来.