一、
常规来说,创建线程的方式有四种,即:继承Thread类重新run方法、实现Runnable接口重写run方法、实现Callable接口重写call方法、以及线程池-Executor。
二、继承Thread类重新run方法
继承Thread类,重写run方法
import java.util.Date;
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i<10; i++){
System.out.println("mythread线程正在执行:"+new Date().getTime());
}
}
}
写测试调用类
import com.multithread.thread.MyThread;
import java.util.Date;
public class ThreadCreateDemo {
public static void main(String[] args){
//1.创建自定义线程
MyThread thread = new MyThread();
thread.start();
//2.主线程循环打印
for (int i=0; i<10; i++){
System.out.println("main主线程正在执行:"+new Date().getTime());
}
}
运行结果:

三:实现Runnable接口
实现Runable接口,重写run方法
import java.util.Date;
public class MyRunable implements Runnable {
public void run() {
for (int i=0; i<10; i++){
System.out.println("MyRunnable线程正在执行:"+new Date().getTime());
}
}
测试类进行调用
import com.multithread.thread.MyRunable;
import com.multithread.thread.MyThread;
import java.util.Date;
public class ThreadCreateDemo {
public static void main(String[] args){
//1.创建自定义线程
Thread thread = new Thread(new MyRunable());
thread.start();
//2.主线程循环打印
for (int i=0; i<10; i++){
System.out.println("main主线程正在执行:"+new Date().getTime());
}
}
运行结果:

四、实现Callable接口
实现Callable接口,重新call方法
import java.util.Date;
import java.util.concurrent.Callable;
public class MyCallable implements Callable<String> {
public String call() throws Exception {
for (int i=0; i<10; i++){
System.out.println("MyCallable正在执行:"+new Date().getTime());
}
return "MyCallable执行完毕!";
}
测试调用类
import com.multithread.thread.MyCallable;
import com.multithread.thread.MyRunable;
import com.multithread.thread.MyThread;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ThreadCreateDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask task = new FutureTask(new MyCallable());
Thread thread = new Thread(task);
thread.start();
for (int i=0; i<10; i++){
System.out.println("main主线程正在执行:"+new Date().getTime());
}
System.out.println(task.get());
}
运行结果:

五、线程池-Executor
实现Runnable接口,重写run方法
import java.util.Date;
public class MyRunable implements Runnable {
public void run() {
for (int i=0; i<10; i++){
System.out.println("MyRunnable线程正在执行:"+new Date().getTime());
}
}
}
测试调用类
import com.multithread.thread.MyCallable;
import com.multithread.thread.MyRunable;
import com.multithread.thread.MyThread;
import java.util.Date;
import java.util.concurrent.*;
public class ThreadCreateDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//1.使用Executors创建线程池
ExecutorService executorService = Executors.newFixedThreadPool(10);
//2.通过线程池执行线程
executorService.execute(new MyRunable());
//3.主线程循环打印
for (int i=0; i<10; i++){
System.out.println("main主线程正在执行:"+new Date().getTime());
}
}
}
六、各个比较
1、实现接口和继承Thread类比较
1.1接口更适合多个相同的程序代码的线程去共享同一个资源。
1.2接口可以避免java中的单继承的局限性。
1.3接口代码可以被多个线程共享,代码和线程独立。
1.4线程池只能放入实现Runable或Callable接口的线程,不能直接放入继承Thread的类。
2、Runnable和Callable接口比较
相同点:
2.11两者都是接口;
2.12两者都可用来编写多线程程序;
2.13两者都需要调用Thread.start()启动线程;
不同点:
2.21实现Callable接口的线程能返回执行结果;而实现Runnable接口的线程不能返回结果;
2.22Callable接口的call()方法允许抛出异常;而Runnable接口的run()方法的不允许抛异常;
2.23实现Callable接口的线程可以调用Future.cancel取消执行 ,而实现Runnable接口的线程不能
注:Callable接口支持返回执行结果,此时需要调用FutureTask.get()方法实现,此方法会阻塞主线程直到获取‘将来’结果;当不调用此方法时,主线程不会阻塞!
本文详细介绍了Java中创建线程的四种主要方法:继承Thread类并重写run方法,实现Runnable接口并重写run方法,实现Callable接口并重写call方法,以及使用线程池Executor。对比了各种方式的优缺点,包括资源共享、代码复用、异常处理和执行结果返回等方面。
849

被折叠的 条评论
为什么被折叠?



