博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 多线程(1)-Thread和Runnable
阅读量:7194 次
发布时间:2019-06-29

本文共 3439 字,大约阅读时间需要 11 分钟。

一提到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接口。

publicclass Thread implements Runnable {    /* Make sure registerNatives is the first thing 
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.

 

 

转载地址:http://cxqkm.baihongyu.com/

你可能感兴趣的文章
SHELL脚本自动部署KVM虚拟化
查看>>
Excel中的IF函数超过7层嵌套的处理
查看>>
使用Docx4J生成 html
查看>>
鼠标移动到图片改变图片
查看>>
linux 标准I/O函数详解
查看>>
Java从SE到EE之eclipse常用快捷键(手稿)
查看>>
AD用户修改密码,新旧密码都可以使用问题
查看>>
了解--Dashboard Android用户自定义UI设计模板
查看>>
office2013如何使用scanning功能,扫描文件
查看>>
centos 操作
查看>>
搭建Ruby on Rails环境 mac
查看>>
C查找字符串
查看>>
Oracle\PLSQL Developer报“动态执行表不可访问,本会话的自动统计被禁止”的解决方案...
查看>>
外排序(大文件内存不够无法一次加载)
查看>>
linux tar
查看>>
bash socket 编程
查看>>
Linux添加新盘扩容空间
查看>>
Java 垃圾回收器
查看>>
spring 事务配置
查看>>
Linux编辑器vi使用方法详细介绍
查看>>