基本的是两种:
第一种是继承Tread class:
class PrimeThread extends Thread { long minPrimePrimeThread(long minPrime) { this.minPrime = minPrime} public void run() { // compute primes larger than minPrime . . . } }
在main里:
PrimeThread p = new PrimeThread(143)p.start()
还有就一种是implements Runnable:
public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a thread!")} public static void main(String args[]) { (new Thread(new HelloRunnable())).start()}}
同样用 xxx.start() 可以运行这个线程
这是基本的,还有就是管理一群线程(threads pool),可以用executor以及executor service, 以上信息都可以在oracle网找到很多例子