Java 多執行緒:extends Thread Class & implements Runnable Interface
阿新 • • 發佈:2019-02-11
1. extends Thread Class
2. implements Runnable Interface
- using .start() function to run Thread
Example:
2. Override public void run() method
3. take advantage of Thread(Runnable), initialize a Thread object
Example:
Runnable Interface 只有public void run()一個介面,沒有start()介面。
而Thread類有Constructor:public Thread(Runnable),
因此,實現Runnable的類需要藉助Thread的該Constructor來例項化,才能使用start()。
在實際開發中一個多執行緒的操作很少使用Thread類,而是通過Runnable介面完成。
在程式開發中只要是多執行緒肯定永遠以實現Runnable介面為主,因為實現Runnable介面相比
繼承Thread類有如下好處:
1. 避免點繼承的侷限(only support extends single class),一個類可以繼承多個介面。
e.g.
2. 適合於資源的共享
e.g.
...Demo2 implements Runnable...
ref:
2. implements Runnable Interface
3. Runnable is more flexible than Thread
[ extends Thread Class ]
- extends Thread Class public class DemoClass extends Thread(){
//class definition
}
- Override "public void run(){}" method
@Override
public void run(){
//function definition
}
- using .start() function to run Thread
DemoClass d1 = new DemoClass(); d1.start(); DemoClass d2 = new DemoClass(); d2.start();
Example:
public class MyThread2 extends Thread{ /* Since java only support extends only 1 class, * MyThread2 can not act as other class's subclass*/ String name; int time_pause; MyThread2(String name, int time_pause){ this.name = name; this.time_pause = time_pause; } /* * override run() from super class */ @Override public void run(){ while(true){ System.out.println(name); try { Thread.sleep(time_pause); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args){ /*directly use the Class "MyThread2" we defined before*/ MyThread2 mt1 = new MyThread2("fast",1000); mt1.start(); MyThread2 mt2 = new MyThread2("slow", 3000); mt2.start(); } }
[ implements Runnable Interface ]
1. implement Runnable Interfacepublic Demo implements Runnable {}
2. Override public void run() method
@Override
public void run() {}
3. take advantage of Thread(Runnable), initialize a Thread object
public class Demo implements Runnable... Thread t = new Thread(new Demo()); t.start(); Demo m = new Demo(); Thread t2 = new Thread(m); t2.start();
Example:
public class MyThread implements Runnable{
/*
* Implements Runnable is more preferred.
* A class can implement multiple interfaces,
* but can only extends single calss*/
int pause_time;
String name;
MyThread(int pause_time, String name){
this.pause_time=pause_time;
this.name = name;
}
@Override
public void run() {
while(true){
System.out.println(name+":"+new Date(System.currentTimeMillis()));
try {
Thread.sleep(pause_time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
/*
* can not create the class directly,
* initialize the class using Thread class*/
Thread t1 = new Thread(new MyThread(1000, "fast"));
t1.start();
Thread t2 = new Thread(new MyThread(3000, "slow"));
t2.start();
}
}
Runnable Interface 只有public void run()一個介面,沒有start()介面。
而Thread類有Constructor:public Thread(Runnable),
因此,實現Runnable的類需要藉助Thread的該Constructor來例項化,才能使用start()。
[ Runnable is more flexible than Thread ]
Thread是實現Runnable的一個子類。在實際開發中一個多執行緒的操作很少使用Thread類,而是通過Runnable介面完成。
在程式開發中只要是多執行緒肯定永遠以實現Runnable介面為主,因為實現Runnable介面相比
繼承Thread類有如下好處:
1. 避免點繼承的侷限(only support extends single class),一個類可以繼承多個介面。
e.g.
public class Demo1 extends Thread {} //extends singel class
public class Demo2 implements Runnable, OtherInterface {} //implements multiple interfaces
2. 適合於資源的共享
e.g.
...Demo1 extends Thread...
Demo1 d1 = new Demo1();
d1.start();
Demo1 d2 = new Demo1();
d2.start();
//d1 d2 are seperate threads, share no data.
...Demo2 implements Runnable...
Demo2 d = new Demo2();
Thread d1 = new Thread(d);
d1.start();
Thread d2 = new Thread(d);
d2.start();
// d1 & d2 using same d, they share d.
ref:
blog.csdn.net/wwww1988600/article/details/7309070