在Java中可通过三种方式来实现多线程:
1、继承Thread类,重写run( )方法
2、实现Runnable接口,重写run( )方法
3、实现Callable接口,重写call( )方法并使用FutureTask获取call( )方法的返回结果
利用Thread类实现多线程
主要步骤
1、通过继承Thread创建线程的子类
2、重写Thread类的run( )方法
3、创建子类实例对象
4、调用子类实例对象start( )方法启动线程
使用示例
package com.thread1;
/**
public class ThreadCreate1 {
public static void main(String[] args) {
//创建第一个子线程
MyThread firstThread=new MyThread();
firstThread.start();
String firstThreadName = firstThread.getName();
System.out.println(“firstThreadName=”+firstThreadName);
//创建第二个子线程
MyThread secondThread=new MyThread();
secondThread.start();
String secondThreadName = secondThread.getName();
System.out.println(“secondThreadName=”+secondThreadName);
}
}
//MyThread继承Thread类
class MyThread extends Thread{
@Override
public void run() {
super.run();
String threadName = Thread.currentThread().getName();
for(int i=0;i<5;i++) {
System.out.println(threadName+",i="+i);
}
}
}
运行结果
利用Runnable接口实现多线程
主要步骤
1、创建Runnable接口实现类并重写该接口的run( )方法
2、创建Runnable接口实现类对象
3、利用Thread有参构造函数public Thread(Runnable target)和Runnable接口实现类对象创建线程实例
4、调用线程实例的start( )方法启动线程
使用示例
package com.thread1;
/**
public class ThreadCreate2 {
public static void main(String[] args) {
// 创建第一个子线程
MyRunnable firstMyRunnable=new MyRunnable();
Thread firstThread = new Thread(firstMyRunnable);
firstThread.start();
String firstThreadName = firstThread.getName();
System.out.println(“firstThreadName=” + firstThreadName);
// 创建第二个子线程
MyRunnable secondMyRunnable=new MyRunnable();
Thread secondThread = new Thread(secondMyRunnable);
secondThread.start();
String secondThreadName = secondThread.getName();
System.out.println(“secondThreadName=” + secondThreadName);
}
}
//MyRunnable实现Runnable接口
class MyRunnable implements Runnable {
@Override
public void run() {
String threadName = Thread.currentThread().getName();
for (int i = 0; i < 5; i++) {
System.out.println(threadName + “,i=” + i);
}
}
}
运行结果
利用Callable接口实现多线程
主要步骤
1、创建Callable接口实现类并重写该接口的call( )方法
2、创建Callable接口实现类对象
3、使用Runnable子类FutureTask的有参构造函数public FutureTask(Callable< V > callable)和Callable接口实现类对象创建FutureTask实例
4、利用Thread有参构造函数public Thread(Runnable target)和FutureTask实例创建线程实例
5、调用线程实例的start( )方法启动线程
6、利用FutureTask的get( )方法获取子线程执行结果
使用示例
package com.thread1;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/*
*/
public class ThreadCreate3 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
// 创建第一个子线程
MyCallable firstMyCallable = new MyCallable();
FutureTask firstFutureTask = new FutureTask<>(firstMyCallable);
Thread firstThread = new Thread(firstFutureTask);
firstThread.start();
String firstThreadName = firstThread.getName();
System.out.println(“firstThreadName=” + firstThreadName);
// 获取第一个子线程返回的结果
Object firstThreadResult = firstFutureTask.get();
System.out.println(“firstThreadResult=” + firstThreadResult);
// 创建第二个子线程
MyCallable secondMyCallable = new MyCallable();
FutureTask secondFutureTask = new FutureTask<>(secondMyCallable);
Thread secondThread = new Thread(secondFutureTask);
secondThread.start();
String secondThreadName = secondThread.getName();
System.out.println(“secondThreadName=” + secondThreadName);
// 获取第二个子线程返回的结果
Object secondThreadResult = secondFutureTask.get();
System.out.println(“secondThreadResult=” + secondThreadResult);
}
}
// MyCallable实现Callable接口
class MyCallable implements Callable {
@Override
public Object call() throws Exception {
String threadName = Thread.currentThread().getName();
int i = 0;
while (i < 5) {
System.out.println(threadName + “,i=” + i);
i++;
}
return i;
}
}
运行结果
总结
为避免Java单继承带来的局限性,在实际开发中多采用实现Runnable接口或实现Callable接口的方式实现多线程。