一提到Java多线程,首先想到的是Thread继承和Runnable的接口实现

Thread继承

public class MyThread extends Thread {
public void run(){
int i = 0;
System.out.println("--------------"+i++);
}
}

Runnable接口实现

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接口。

public
class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit> does. */
private static native void registerNatives();
static {
registerNatives();
} private char name[];
private int priority;
private Thread threadQ;
private long eetop;
......
}

那么Thread 和Runnabe 有什么区别呢?

The most common difference is

  • When you extends Thread class, 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 you implements Runnable, you can save a space for your class to extend any other class in future or now.

However, the significant difference is.

  • When you extends Thread class, each of your thread creates unique object and associate with it.
  • When you implements Runnable, it shares the same object to multiple threads.

Thread vs Runnable

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 of counter is 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 separate counter, 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.

最新文章

  1. 安卓开发之ListAdapter(二)
  2. 知方可补不足~Sqlserver发布订阅与sql事务的关系
  3. ionic 添加地图定位功能
  4. Android——进度对话框
  5. Github上最全的APICloud开源前端框架效果盘点(转)
  6. (转)Salesforce的440亿美金并购宣告企业软件市场进入3.0互联网化时代
  7. google guava 基本工具
  8. 江中微型统计分析软件V1.0版本完成
  9. php5.4安装ecshopphp5.4问题及解决
  10. sqlserver存储过程及易错点
  11. 在阿里云 ECS 搭建 nginx https nodejs 环境 (一、 nginx)
  12. JavaWeb学习日记----DTD
  13. flask 压缩json
  14. Java中十个常见的违规编码
  15. hibernate 和mybatis
  16. Python 自然语言处理笔记(一)
  17. nginx负载均衡算法
  18. SQLSERVER中的元数据锁
  19. KVM源代码解读:linux-3.17.4\include\linux\kvm_host.h
  20. JavaScript常用设计模式

热门文章

  1. EasyUI 添加tab页(iframe方式)(转)
  2. 不要轻易delete void*指针,这样会隐藏比较多的错误。
  3. 今天开始着手原来Office系统的重构
  4. MySQL Connector/J 6.x jdbc.properties 配置, mysql-connector-java-6.0.4.jar 异常
  5. Yaf零基础学习总结3-Hello Yaf
  6. Big Event in HDU(HDU1171)可用背包和母函数求解
  7. linux-7 man 命令
  8. orcale 匿名代码块
  9. javaweb 乱码---汉字存入mysql数据库中变成乱码
  10. Linux下oracle11gR2系统安装到数据库建立配置及最后oracle的dmp文件导入一站式操作记录