java啟動執行緒時 extends與implements的一個差異

滄海一滴發表於2013-11-01

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

 
 
 





 
 
 
 

相關文章