一提到Java多线程,首先想到的是Thread继承和Runnable的接口实现
Thread继承
1
2
3
4
5
6
|
public class MyThread extends Thread {
public void run(){
int i = 0 ;
System.out.println( "--------------" +i++);
}
}
|
Runnable接口实现
1
2
3
4
5
6
7
8
9
|
public class RunnableImpl implements Runnable {
private long value = 0 ;
@Override
public synchronized void run() {
while (ThreadMain.tickets > 0 ){
System.out.println(Thread.currentThread().getName()+ "------------" + --ThreadMain.tickets);
}
}
}
|
两者都可以实现多线程程序的创建。实际上,我们查看Thread的代码实现,也可以发现,Thread实际上也是实现了Runnable接口。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit> does. */
private static native void registerNatives();
static {
registerNatives();
|
那么Thread 和Runnabe 有什么区别呢?
The most common difference is
- When youextends Threadclass, after that you can’t extend any other class which you required. (As you know, Java does not allow inheriting more than one class).
- When youimplements Runnable, you can save a space for your class to extend any other class in future or now.
However, the significant difference is.
- When youextends Threadclass, each of your thread creates unique object and associate with it.
- When youimplements Runnable, it shares the same object to multiple threads.
Thread vs Runnable
1
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
class ImplementsRunnable implements Runnable {
private int counter = 0 ;
public void run() {
counter++;
System.out.println( "ImplementsRunnable : Counter : " + counter);
}
}
class ExtendsThread extends Thread {
private int counter = 0 ;
public void run() {
counter++;
System.out.println( "ExtendsThread : Counter : " + counter);
}
}
public class ThreadVsRunnable {
public static void main(String args[]) throws Exception {
// Multiple threads share the same object.
ImplementsRunnable rc = new ImplementsRunnable();
Thread t1 = new Thread(rc);
t1.start();
Thread.sleep( 1000 ); // Waiting for 1 second before starting next thread
Thread t2 = new Thread(rc);
t2.start();
Thread.sleep( 1000 ); // Waiting for 1 second before starting next thread
Thread t3 = new Thread(rc);
t3.start();
// Creating new instance for every thread access.
ExtendsThread tc1 = new ExtendsThread();
tc1.start();
Thread.sleep( 1000 ); // Waiting for 1 second before starting next thread
ExtendsThread tc2 = new ExtendsThread();
tc2.start();
Thread.sleep( 1000 ); // Waiting for 1 second before starting next thread
ExtendsThread tc3 = new ExtendsThread();
tc3.start();
}
}
|
执行结果输出如下:
ImplementsRunnable : Counter : 1
ImplementsRunnable : Counter : 2
ImplementsRunnable : Counter : 3
ExtendsThread : Counter : 1
ExtendsThread : Counter : 1
ExtendsThread : Counter : 1
In the Runnable interface approach, only one instance of a class is being created and it has been shared by different threads. So the value ofcounteris incremented for each and every thread access.
Whereas, Thread class approach, you must have to create separate instance for every thread access. Hence different memory is allocated for every class instances and each has separatecounter, the value remains same, which means no increment will happen because none of the object reference is same.
Which one is best to use?
Ans : Very simple, based on your application requirements you will use this appropriately. But I would suggest, try to use interface inheritance i.e., implements Runnable.
原文地址:https://www.cnblogs.com/CBDoctor/p/5077981.html
相关文章
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-25 59
-
2025-05-25 46
-
2025-06-04 74
-
2025-05-29 64
-
2025-06-05 44