方式一:继承Thread类并重写run()方法。
public class CreatingThread01 extends Thread {@Overridepublic void run() {System.out.println(getName() + " is running");}public static void main(String[] args) {new CreatingThread01().start();new CreatingThread01().start();new CreatingThread01().start();new CreatingThread01().start();}}public class CreatingThread01 extends Thread { @Override public void run() { System.out.println(getName() + " is running"); } public static void main(String[] args) { new CreatingThread01().start(); new CreatingThread01().start(); new CreatingThread01().start(); new CreatingThread01().start(); } }public class CreatingThread01 extends Thread { @Override public void run() { System.out.println(getName() + " is running"); } public static void main(String[] args) { new CreatingThread01().start(); new CreatingThread01().start(); new CreatingThread01().start(); new CreatingThread01().start(); } }
采用继承Thread类方式
- 优点: 编写简单,如果需要访问当前线程,无需使用Thread.currentThread ()方法,直接使用this,即可获得当前线程
- 缺点:因为线程类已经继承了Thread类,所以不能再继承其他的父类
![创建线程的方式有哪几种 图片[1]-创建线程的方式有哪几种-不念博客](https://www.bunian.cn/wp-content/uploads/2024/03/7ef9f644-6b7b-44c6-a524-573c8112713b.png)
方式二:实现Runnable接口并实现run()方法,然后将实现了Runnable接口的类传递给Thread类。
public class CreatingThread02 implements Runnable {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + " is running");}public static void main(String[] args) {new Thread(new CreatingThread02()).start();new Thread(new CreatingThread02()).start();new Thread(new CreatingThread02()).start();new Thread(new CreatingThread02()).start();}}public class CreatingThread02 implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + " is running"); } public static void main(String[] args) { new Thread(new CreatingThread02()).start(); new Thread(new CreatingThread02()).start(); new Thread(new CreatingThread02()).start(); new Thread(new CreatingThread02()).start(); } }public class CreatingThread02 implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + " is running"); } public static void main(String[] args) { new Thread(new CreatingThread02()).start(); new Thread(new CreatingThread02()).start(); new Thread(new CreatingThread02()).start(); new Thread(new CreatingThread02()).start(); } }
采用实现Runnable接口方式:
- 优点:线程类只是实现了Runable接口,还可以继承其他的类。在这种方式下,可以多个线程共享同一个目标对象,所以非常适合多个相同线程来处理同一份资源的情况,从而可以将CPU代码和数据分开,形成清晰的模型,较好地体现了面向对象的思想。
- 缺点:编程稍微复杂,如果需要访问当前线程,必须使用Thread.currentThread()方法。
方式三:使用Callable和Future接口通过Executor框架创建线程。
public class CreatingThread03 implements Callable<Long> {@Overridepublic Long call() throws Exception {Thread.sleep(2000);System.out.println(Thread.currentThread().getId() + " is running");return Thread.currentThread().getId();}public static void main(String[] args) throws ExecutionException, InterruptedException {FutureTask<Long> task = new FutureTask<>(new CreatingThread03());new Thread(task).start();System.out.println("等待完成任务");Long result = task.get();System.out.println("任务结果:" + result);}}public class CreatingThread03 implements Callable<Long> { @Override public Long call() throws Exception { Thread.sleep(2000); System.out.println(Thread.currentThread().getId() + " is running"); return Thread.currentThread().getId(); } public static void main(String[] args) throws ExecutionException, InterruptedException { FutureTask<Long> task = new FutureTask<>(new CreatingThread03()); new Thread(task).start(); System.out.println("等待完成任务"); Long result = task.get(); System.out.println("任务结果:" + result); } }public class CreatingThread03 implements Callable<Long> { @Override public Long call() throws Exception { Thread.sleep(2000); System.out.println(Thread.currentThread().getId() + " is running"); return Thread.currentThread().getId(); } public static void main(String[] args) throws ExecutionException, InterruptedException { FutureTask<Long> task = new FutureTask<>(new CreatingThread03()); new Thread(task).start(); System.out.println("等待完成任务"); Long result = task.get(); System.out.println("任务结果:" + result); } }
采用实现Callable接口方式:
- 缺点:编程稍微复杂,如果需要访问当前线程,必须调用Thread.currentThread()方法。
- 优点:线程只是实现Runnable或实现Callable接口,还可以继承其他类。这种方式下,多个线程可以共享一个target对象,非常适合多线程处理同一份资源的情形。
© 版权声明
本站文章由不念博客原创,未经允许严禁转载!
THE END