java启动线程时 extends与implements的一个差异

java extends与implements在使用时的一个差异:

Implements:

public class ThreadImplementsTest  implements Runnable{
    
    public void loop(){
        String name=Thread.currentThread().getName();
        System.out.println(name+"====>进入loop()");
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                System.out.println(e);
            }
        }
        System.out.println(name+"=============>离开Loop");
    }
    

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ThreadSleepTest tst=new ThreadSleepTest();
        tst.run();
        
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        tst.loop();
    }

    public void run() {
        // TODO Auto-generated method stub
        loop();
    }
}
Output:

main====>进入loop()
main=============>离开Loop
main====>进入loop()
main=============>离开Loop

可以看出直接在ThreadImplementsTest 中调用run方法是没有多线程的,原因是实现Runnable接口的类,只是说明具备了多线程的能力,要在多线程下运行,需要给一个环境(机会):

可使用以下两种方法来调用:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadImplTest {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ThreadImplementsTest tit=new ThreadImplementsTest();
//        new Thread(tit).start();
//        tit.loop();
    //或
        //推荐使用此方式调用
        ExecutorService es=Executors.newCachedThreadPool(); 
        es.execute(tit);
        tit.loop();
    }
}
Output:
//new Thread(tit).start();

main====>进入loop()
Thread-0====>进入loop()
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
Thread-0=============>离开Loop
main=============>离开Loop

 

//        ExecutorService es=Executors.newCachedThreadPool(); 
//        es.execute(tit);

pool-1-thread-1====>进入loop()
main====>进入loop()
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
pool-1-thread-1=============>离开Loop
main=============>离开Loop

 

Extends:
public class ThreadExtendsTest extends Thread {
    public void loop(){
        String name=Thread.currentThread().getName();
        System.out.println(name+"====>进入loop()");
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                System.out.println(e);
            }
        }
        System.out.println(name+"=============>离开Loop");
    }
    
    public void run() {
        // TODO Auto-generated method stub
        loop();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ThreadExtendsTest tet=new ThreadExtendsTest();
        tet.setName("Test Thread");
        tet.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        tet.loop();
        
    }

}
Output:

Test Thread====>进入loop()
main====>进入loop()
Test Thread=============>离开Loop
main=============>离开Loop

 
 
 





 
 
 
 
posted @ 2013-11-01 16:44  沧海一滴  阅读(517)  评论(3编辑  收藏  举报