java extends与implements在使用时的一个差异:
Implements:
public class ThreadImplementsTest implements Runnable{public void loop(){String name&#61;Thread.currentThread().getName();System.out.println(name&#43;"&#61;&#61;&#61;&#61;>进入loop()");for (int i &#61; 0; i <10; i&#43;&#43;) {try {Thread.sleep(200);} catch (InterruptedException e) {// TODO Auto-generated catch blockSystem.out.println(e);}}System.out.println(name&#43;"&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>离开Loop");}/**
* &#64;param args
*/public static void main(String[] args) {// TODO Auto-generated method stubThreadSleepTest tst&#61;new ThreadSleepTest();tst.run();try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}tst.loop();}public void run() {// TODO Auto-generated method stubloop();}
}
Output:
main&#61;&#61;&#61;&#61;>进入loop()
main&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>离开Loop
main&#61;&#61;&#61;&#61;>进入loop()
main&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>离开Loop
可以看出直接在ThreadImplementsTest 中调用run方法是没有多线程的&#xff0c;原因是实现Runnable接口的类&#xff0c;只是说明具备了多线程的能力&#xff0c;要在多线程下运行&#xff0c;需要给一个环境&#xff08;机会&#xff09;&#xff1a;
可使用以下两种方法来调用&#xff1a;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadImplTest {/**
* &#64;param args
*/public static void main(String[] args) {// TODO Auto-generated method stubThreadImplementsTest tit&#61;new ThreadImplementsTest();
// new Thread(tit).start();
// tit.loop();//或//推荐使用此方式调用ExecutorService es&#61;Executors.newCachedThreadPool(); es.execute(tit);tit.loop();}
}
Output:
//new Thread(tit).start();
main&#61;&#61;&#61;&#61;>进入loop()
Thread-0&#61;&#61;&#61;&#61;>进入loop()
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
Thread-0&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>离开Loop
main&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>离开Loop
// ExecutorService es&#61;Executors.newCachedThreadPool();
// es.execute(tit);
pool-1-thread-1&#61;&#61;&#61;&#61;>进入loop()
main&#61;&#61;&#61;&#61;>进入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&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>离开Loop
main&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>离开Loop
Extends:
public class ThreadExtendsTest extends Thread {public void loop(){String name&#61;Thread.currentThread().getName();System.out.println(name&#43;"&#61;&#61;&#61;&#61;>进入loop()");for (int i &#61; 0; i <10; i&#43;&#43;) {try {Thread.sleep(200);} catch (InterruptedException e) {// TODO Auto-generated catch blockSystem.out.println(e);}}System.out.println(name&#43;"&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>离开Loop");}public void run() {// TODO Auto-generated method stubloop();}/**
* &#64;param args
*/public static void main(String[] args) {// TODO Auto-generated method stubThreadExtendsTest tet&#61;new ThreadExtendsTest();tet.setName("Test Thread");tet.start();try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}tet.loop();}}
Output:
Test Thread&#61;&#61;&#61;&#61;>进入loop()
main&#61;&#61;&#61;&#61;>进入loop()
Test Thread&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>离开Loop
main&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>离开Loop