摘要:本文整理了创建线程的几种方式。
环境
Windows 10 企业版 LTSC 21H2
Java 1.8
1 继承Thread类
步骤:
- 创建继承Thread类的子类,重写
run()
方法。
- 创建子类的Thread对象。
- 调用Thread对象的
start()
方法。
示例:
java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class Demo { public static void main(String[] args) { Thread t1 = new DemoThread("线程一"); Thread t2 = new DemoThread("线程二"); t1.start(); t2.start(); } }
class DemoThread extends Thread { public DemoThread(String name) { this.setName(name); }
@Override public void run() { for (int i = 0; i < 1000; i++) { System.out.println(Thread.currentThread().getName() + " >>> " + i); } } }
|
2 实现Runnable接口
步骤:
- 创建实现Runnable接口的类,实现
run()
方法。
- 创建实现类的Runnable对象。
- 将Runnable对象传入Thread类的构造方法中,创建Thread对象。
- 调用Thread对象的
start()
方法。
示例:
java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class Demo { public static void main(String[] args) { DemoRunnable runnable = new DemoRunnable(); Thread t1 = new Thread(runnable, "线程一"); Thread t2 = new Thread(runnable, "线程二"); t1.start(); t2.start(); } }
class DemoRunnable implements Runnable { @Override public void run() { for (int i = 0; i < 1000; i++) { System.out.println(Thread.currentThread().getName() + " >>> " + i); } } }
|
开发中优先选择实现Runnable接口的方式,没有单继承的局限性,更适合处理多个线程共享数据的情况。
3 实现Callable接口
步骤:
- 创建实现Callable接口的类,实现
call()
方法。
- 创建实现类的Callable对象。
- 将Callable对象传入FutureTask类的构造方法中,创建FutureTask对象。
- 将FutureTask对象传入Thread类的构造方法中,创建Thread对象。
- 调用Thread对象的
start()
方法。
- 调用FutureTask对象的
get()
方法,获取执行结果。
示例:
java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class Demo { public static void main(String[] args) { DemoCallable callable = new DemoCallable(); FutureTask<Integer> f1 = new FutureTask<>(callable); FutureTask<Integer> f2 = new FutureTask<>(callable); Thread t1 = new Thread(f1, "线程一"); Thread t2 = new Thread(f2, "线程二"); t1.start(); t2.start(); try { System.out.println(f1.get() + f2.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }
class DemoCallable implements Callable<Integer> { @Override public Integer call() throws Exception { for (int i = 0; i < 1000; i++) { System.out.println(Thread.currentThread().getName() + " >>> " + i); } return 1000; } }
|
FutureTask类间接实现了Runnable接口和Future接口,所以既可以作为Runnable被线程执行,又可以作为Future获取线程执行的结果。
4 使用线程池
步骤:
- 创建实现Runnable接口的类,实现
run()
方法。
- 创建线程池的ThreadPoolExecutor对象。
- 创建实现类的Runnable对象。
- 将Runnable对象传入ThreadPoolExecutor对象的
execute()
方法中,提交任务。
- 调用ThreadPoolExecutor对象的
shutdown()
方法关闭线程池。
示例:
java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public class Demo { public static void main(String[] args) { ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 100, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(3)); for (int i = 1; i <= 7; i++) { Runnable runnable = new DemoThread(i); executor.execute(runnable); System.out.println("线程编号:" + i + ",线程池:" + executor.getPoolSize() + ",队列:" + executor.getQueue().size()); } executor.shutdown(); } }
class DemoThread implements Runnable { int taskNo = 0;
public DemoThread(int taskNo) { this.taskNo = taskNo; }
@SuppressWarnings("static-access") public void run() { try { System.out.println("task " + taskNo); Thread.currentThread().sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
|
条