Java创建多线程的两种方式对比
作者:hebedich 时间:2023-09-14 21:02:49
采用继承Thead类实现多线程:
优势:编写简单,如果需要访问当前线程,只需使用this即可,无需使用Thead.currentThread()方法。
劣势:因为这种线程类已经继承了Thead类,所以不能再继承其它类。
示例代码:
package org.frzh.thread;
public class FirstThread extends Thread{
private int i;
//重写run方法,run方法的方法体就是线程执行体
public void run() {
for (; i < 100; i++) {
//当线程类继承Thread类时,可以直接调用getName方法获得当前线程名
//如果想获得当前线程,直接使用this
//Thread对象的getName方法返回当前线程的名字
System.out.println(getName() + " " + i);
}
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
//调用Thead的currentThread方法获取当前线程
System.out.println(Thread.currentThread().getName() + " " +i);
if (i == 20) {
new FirstThread().start();
new FirstThread().start();
}
}
}
}
运行结果片段:
我们发现,在两个子线程中i的值并不连续,似乎与我们说的子线程直接共享数据不符。其实,在这里我们实例化了两个子线程,每个拥有自己的实例变量i。
采用实现Runable接口的多线程:
优势:线程类只是实现了Runable接口,因此还可以继承其他类;
在这种情况下,可以使多个线程共享一个target对象,所以非常适合多个线程用来处理同一份资源的情况,从而可以将cpu、代码和数据分开,形成清晰的模型,较好的体现面向对象思想。
劣势:编程略有些复杂,如果要访问当前线程必须使用Thread.currentThread方法。
示例代码:
package org.frzh.thread;
public class SecondThread implements Runnable{
private int i;
@Override
public void run() {
// TODO Auto-generated method stub
for (; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
if (i == 20) {
SecondThread st = new SecondThread();
new Thread(st, "子线程1").start();
new Thread(st, "子线程2").start();
}
}
}
}
运行结果片段:
可以看到,此时的i值是连续变化的,因为线程1和2共享同一个target。
标签:Java,多线程,对比
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android项目实现视频播放器
2022-10-05 07:53:35
![](https://img.aspxhome.com/file/2023/1/129141_0s.jpg)
Android ItemDecoration 实现分组索引列表的示例代码
2022-02-10 20:22:12
![](https://img.aspxhome.com/file/2023/1/98661_0s.gif)
Android自定义ProgressBar实现漂亮的进度提示框
2023-04-04 22:05:30
![](https://img.aspxhome.com/file/2023/4/138384_0s.jpg)
详解C#之委托
2021-12-12 00:17:49
![](https://img.aspxhome.com/file/2023/0/79100_0s.png)
Android实现带数字的圆形进度条(自定义进度条)
2023-07-19 21:37:20
![](https://img.aspxhome.com/file/2023/0/139140_0s.gif)
Java实现字符串匹配的示例代码
2023-04-17 05:24:35
使用C#实现阿拉伯数字到大写中文的转换
2023-03-03 16:34:16
![](https://img.aspxhome.com/file/2023/3/122653_0s.jpg)
亲自教你实现栈及C#中Stack源码分析
2021-05-23 19:39:10
![](https://img.aspxhome.com/file/2023/4/79054_0s.jpg)
Java获取文件的类型和扩展名的实现方法
2021-09-24 08:56:56
![](https://img.aspxhome.com/file/2023/3/82373_0s.png)
springboot+jwt+微信小程序授权登录获取token的方法实例
2022-07-11 17:53:57
![](https://img.aspxhome.com/file/2023/5/92725_0s.jpg)
Java SE求解汉诺塔问题的示例代码
2022-05-10 23:44:30
![](https://img.aspxhome.com/file/2023/1/61291_0s.png)
Java实现抠图片文字或签名的完整代码
2023-04-18 00:04:44
Java 深入探讨设计模式之原型模式篇
2023-11-16 17:37:59
![](https://img.aspxhome.com/file/2023/3/64553_0s.png)
java实现马踏棋盘算法(骑士周游问题)
2022-03-17 20:29:46
![](https://img.aspxhome.com/file/2023/1/94361_0s.jpg)
如何使用Spring AOP的通知类型及创建通知
2022-03-19 19:32:51
![](https://img.aspxhome.com/file/2023/2/61242_0s.png)
java获取redis日志信息与动态监控信息的方法
2021-11-19 06:24:55
![](https://img.aspxhome.com/file/2023/3/74763_0s.png)
在Java内存模型中测试并发程序代码
2023-11-24 20:37:55
![](https://img.aspxhome.com/file/2023/7/59967_0s.png)
Java Hibernate对象(瞬时态,持久态,脱管态)详解
2022-05-22 20:07:32
![](https://img.aspxhome.com/file/2023/5/129105_0s.png)
java 多态性详解及常见面试题
2023-03-15 18:08:49
浅谈JVM垃圾回收有哪些常用算法
2022-02-28 16:51:56