首页 技术 正文
技术 2022年11月14日
0 收藏 780 点赞 5,019 浏览 2485 个字

  JAVA中创建线程的方式有三种,各有优缺点,具体如下:

一、继承Thread类来创建线程

1、创建一个任务类,继承Thread线程类,因为Thread类已经实现了Runnable接口,然后重写run()方法,run()方法中的内容就是需要线程完成的任务。

2、创建一个任务类的对象,即创建了线程对象。

3、调用任务类对象的start()方法,来启动一个线程。

代码实例:

 public class TestThread extends Thread {
public void run() {
for (int i = 0; i < 20; i++) {
// 与Thread.currentThread().getName()相同
System.out.println(this.getName() + " " + i);
}
} public static void main(String[] args) {
TestThread t1 = new TestThread();
TestThread t2 = new TestThread();
t1.start();
t2.start();
}
}

二、实现Runnable接口来创建线程

1、创建一个任务类,实现Runnable接口,并实现run()方法,run()方法中的内容就是需要线程完成的任务。

2、创建一个任务类的对象。

3、任务类必须在线程中执行,因此将任务类的对象作为参数,创建一个Thread类对象,该Thread类对象才是真正的线程对象。

4、调用Thread线程类对象的start()方法,来启动一个线程。

代码实例:

 public class TestThread implements Runnable {
public void run() {
for (int i = 0; i < 20; i++) {
// 获取线程名称,默认格式:Thread-0
System.out.println(Thread.currentThread().getName() + " " + i);
}
} public static void main(String[] args) {
TestThread tt1 = new TestThread();
TestThread tt2 = new TestThread();
// 可为线程添加名称:Thread t1 = new Thread(tt1, "线程1");
Thread t1 = new Thread(tt1);
Thread t2 = new Thread(tt2);
t1.start();
t2.start();
}
}

三、通过Callable和Future来创建线程

1、创建一个任务类,实现Callable接口,并实现call()方法,call()方法中的内容就是需要线程完成的任务,且有返回值。

2、创建一个任务类的对象,并使用FutureTask类来包装任务类的对象,该FutureTask对象封装了任务类对象中call()方法的返回值。

3、任务类必须在线程中执行,因此将FutureTask类的对象作为参数,创建一个Thread类对象,该Thread类对象才是真正的线程对象。

4、调用Thread线程类对象的start()方法,来启动一个线程。

5、调用FutureTask类对象的get()方法来获取线程执行的返回值,即任务类对象中call()方法的返回值。

代码实例:

 public class TestThread implements Callable<Integer> {
public Integer call() {
int i = 0;
for (i = 0; i < 20; i++) {
if (i == 5)
break;
System.out.println(Thread.currentThread().getName() + " " + i);
}
return i;
} public static void main(String[] args) {
TestThread tt = new TestThread();
FutureTask<Integer> ft = new FutureTask<Integer>(tt);
Thread t = new Thread(ft);
t.start();
try {
System.out.println(Thread.currentThread().getName() + " " + ft.get());
} catch (Exception e) {
e.printStackTrace();
}
}
}

四、三种方式创建线程的比较

1、继承Thread类方式:

(1)优点:编写简单,任务类中访问当前线程时,可以直接使用this关键字。

(2)缺点:任务类即线程类已经继承了Thread类,所以不能再继承其他父类。

2、实现Runnable接口的方式:

(1)优点:任务类只实现了Runnable接口,还可以继承其他类。这种方式,可以多个线程对象共享一个任务类对象,即多线程共享一份资源的情况,如下:

     TestThread tt1 = new TestThread();
Thread t1 = new Thread(tt1);
Thread t2 = new Thread(tt1);
t1.start();
t2.start();

(2)缺点:编写稍微复杂,任务类中访问当前线程时,必须使用Thread.currentThread()方法。

3、通过Callable和Future的方式:

(1)优点:任务类只实现了Callable接口,还可以继承其他类,同样多线程下可共享同一份资源,这种方式还有返回值,并且可以抛出返回值的异常。

(2)缺点:编写稍微复杂,任务类中访问当前线程时,必须使用Thread.currentThread()方法。

  总结:在仅仅只重写run()方法,而不重写Thread类其他方法的前提下,比较推荐实现Runnable接口的方式创建线程。因为不打算修改或增强类的基本能力,不应该为其创建子类。而且实现Runnable接口的方式,线程和资源相对分离,程序更加健壮,更符合面向对象的编程思想。当然,需要线程有返回值时可以使用Callable的方式,但Callable的方式有一个问题,当调用get()方法时,如果线程还未执行完毕,则会阻塞到线程执行完毕拿到返回值。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,957
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,480
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,327
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,110
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,742
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,776