在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口

时间:2021-05-14 17:31:08
//继承thread类
class PrimeThread extends Thread{
	long minPrime;
	PrimeThread(long minPrime)
	{
		this.minPrime=minPrime;
	}
	public void run(){
		//computer 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(){
		//compuyer primes larger than minPrime
	}
}
//调用方法
PrimeRun p=new PrimeRun(143);
new Thread(p).start();