public class Thread
extends Object
implements Runnable
Thread通过实现Runnable实现多态关系。
Java中实现多线程,最基本2种方式:
通过继承thread类
class PrimeThread extends Thread {
long minPrime;
PrimeThread(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}
PrimeThread p = new PrimeThread(143);
p.start();
或者
如果你的类已经有了继承的父类,那么,实现多线程就可以通过Runnable接口来实现。
class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}
PrimeRun p = new PrimeRun(143);
new Thread(p).start();