1.继承 Thread 类
通过在类上继承 Thread 类来实现多线程,Thread 类底层本质也是实现 Runnable 接口,它表示一个线程的实例,源代码如下所示:
public
class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit> does. */
private static native void registerNatives();
static {
registerNatives();
}
private volatile String name;
private int priority;
private Thread threadQ;
private long eetop;
Thread 类内部有更丰富的方法,并且启动线程的唯一方法是通过Thread 类的 start() 方法。这种方式有一个缺点就是若当前类也需要继承其他父类则不能满足。因为一个类只能继承一个类,但可以实现多个接口。
代码示例:
public class ThreadClass extends Thread{
@Override
public void run() {
try {
Thread.sleep(100);
System.out.println(Thread.currentThread().getId() + "继承了 Thread 类!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ThreadClass thread = new ThreadClass();
ThreadClass thread1 = new ThreadClass();
thread.start();
thread1.start();
}
}
2.实现 Runnable 接口方式实现多线程
若自己的类已经继承了其他类,就无法继承 Thread 类,此时就需要使用实现 Runnable 接口的方式实现多线程。
代码示例如下:
public class RunnableClass implements Runnable{
@Override
public void run() {
try {
Thread.sleep(100);
System.out.println(Thread.currentThread().getId() + "继承了 Thread 类!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
RunnableClass thread = new RunnableClass();
Thread thread1 = new Thread(thread);
Thread thread2 = new Thread(thread);
thread1.start();
thread2.start();
}
}
3.实现 Callable 接口
在jdk 1.5 以前实现线程只有上面两种方式,自从jdk 1.5 之后提供了Callable 和 Future ,通过他们可以在任务执行完毕后得到任务的执行结果。
代码示例如下:
public class CallableClass implements Callable {
@Override
public String call() throws Exception {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + "继承了 Thread 类!");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "......";
}
public static void main(String[] args) throws Exception {
CallableClass thread = new CallableClass();
FutureTask<String> futureTask = new FutureTask<String>(thread);
// futureTask.run();
String call = thread.call();
// String s = futureTask.get();
System.out.println("结果:" + call);
}
}
其中 FutureTask 实现了 Future 和 Runnable 接口,线程调用可使用 FutureTask.run() 方法执行。但是 Future 是具有阻塞性的,若call方法没有返回结果时,则当前线程一直被阻塞,会影响性能。