明述java中线程(thread)的使用(2)

下面举一个实实在在的例子:
class SingleThread{
    /** Creates a new instance of SingleThread */
    public SingleThread() {
    }
    public static void main(String[] args) throws InterruptedException{
        for(int i=0; i<10;i++){
            Threadclass1 thread1 = new Threadclass1();
            thread1.sleep(1000);
            Threadclass2 thread2 = new Threadclass2(); 
            thread2.sleep(1000);
        }
    }
}
class Threadclass1 extends Thread{
    public Threadclass1(){
        start();// start() 写在这里,这样对象一生成线程就处于准备状态了
    }
    public void run(){
        System.out.println("this is thread1");
    }
}
class Threadclass2 extends Thread{
    public Threadclass2(){
        start();
    }
    public void run(){
        System.out.println("this is thread2");
    }
}
结果:
this is thread1
this is thread2
this is thread1
this is thread2
this is thread1
this is thread2
this is thread1
this is thread2
this is thread1
this is thread2
this is thread1
this is thread2
this is thread1
上面的结果是一秒钟出一条。
sleep() 的功能是说“你,给我先停一会儿”
但线程滞所以是线程,在于互不干扰,共同运行,在 thread1 停下来的时候, thread2 可是继续运行的。
看到这如果你没有晕过去,那么就恭喜你了。那么线程其他功能又如何使用呢?
欲知后事如何,且听下回分解。

你可能感兴趣的:(java,线程,职场,使用,休闲)