前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 多线程之 Runnable VS Thread 及其资源共享问题

Java 多线程之 Runnable VS Thread 及其资源共享问题

作者头像
用户1177713
发布2018-02-24 17:17:44
8410
发布2018-02-24 17:17:44
举报
文章被收录于专栏:数据之美数据之美

对于 Java 多线程编程中的 implements Runnable 与 extends Thread,部分同学可能会比较疑惑,它们之间究竟有啥区别和联系呢?他们是不是没啥区别随便选呢?实际中究竟该选择哪一个呢?

甚至网上不少博客文章以讹传讹得出不少谬论,那今天的走进科学栏目将带您一一揭开谜底。

1、区别:

其实这块主要是围绕着接口和抽象类的区别以及一些设计原则而言的。

1.1 Inheritance Option:   

The limitation with "extends Thread" approach is that if you extend Thread,  you can not extend anything else . Java does not support multiple inheritance.  In reality , you do not need Thread class behavior , because in order to use a thread you need to instantiate one anyway. On the other hand, Implementing the Runnable interface gives you the choice to extend any class you like , but still define behavior that will be run by separate thread.

1.2 Reusability

In "implements Runnable" , we are creating a different Runnable class for a specific behavior  job (if the work you want to be done is job). It gives us the freedom to reuse the specific behavior job whenever required. "extends Thread"  contains both thread and job specific behavior code. Hence once thread completes execution , it can not be restart again.   

1.3 Object Oriented Design:  

Implementing Runnable should be preferred . It does not specializing or modifying the thread behavior . You are giving thread something to run. We conclude that Composition is the better way. Composition means two objects A and B satisfies has-a  relationship. "extends Thread"  is not a good Object Oriented practice.

1.4 Loosely-coupled

"implements Runnable" makes the code loosely-coupled and easier to read . Because the code is split into two classes . Thread class for the thread specific code and your Runnable implementation class for your job that should be run by a thread code. "extends Thread"  makes the code tightly coupled . Single class contains the thread code as well as the job that needs to be done by the thread.

1.5 Functions overhead :  

"extends Thread"  means inheriting all the functions of the Thread class which we may do not need .  job can be done easily by Runnable without the Thread class functions overhead.

至此,个人是推荐优先选择  implements Runnable 。

2、联系:

2.1 其实Thread类也是Runnable接口的子类

代码语言:javascript
复制
public class Thread extends Object implements Runnable

2.2 启动线程都是 start() 方法

追踪Thread中的start()方法的定义,可以发现此方法中使用了private native void start0();其中native关键字表示可以调用操作系统的底层函数,这样的技术称为JNI技术(java Native Interface)。

但是在使用Runnable定义的子类中没有start()方法,只有Thread类中才有。此时观察Thread类,有一个构造方法:public Thread(Runnable targer),此构造方法接受Runnable的子类实例,也就是说可以通过Thread类来启动Runnable实现的多线程。

但是可以看到它们子线程运行的位置不同,Thread运行在父类的run方法中,Runnable运行在实现Runnable接口的子类对象run方法中。

2.3 网传的一种缪论:用Runnable就可以实现资源共享,而 Thread 不可以

有同学的例子是这样的,参考:http://developer.51cto.com/art/201203/321042.htm

代码语言:javascript
复制
package tmp;

class MyThread extends Thread {

	private int ticket = 10;
	private String name;

	public MyThread(String name) {
		this.name = name;
	}

	public void run() {
		for (int i = 0; i < 500; i++) {
			if (this.ticket > 0) {
				System.out.println(this.name + "卖票---->" + (this.ticket--));
			}
		}
	}
}

public class ThreadDemo {

	public static void main(String[] args) {
		MyThread mt1 = new MyThread("一号窗口");
		MyThread mt2 = new MyThread("二号窗口");
		MyThread mt3 = new MyThread("三号窗口");
		mt1.start();
		mt2.start();
		mt3.start();
	}

}

// 一号窗口卖票---->10
// 二号窗口卖票---->10
// 二号窗口卖票---->9
// 二号窗口卖票---->8
// 三号窗口卖票---->10
// 三号窗口卖票---->9
// 三号窗口卖票---->8
...

Runnable 代码:

代码语言:javascript
复制
package tmp;

class MyThread1 implements Runnable {
	private int ticket = 10;
	private String name;

	public void run() {
		for (int i = 0; i < 500; i++) {
			if (this.ticket > 0) {
				System.out.println(Thread.currentThread().getName() + "卖票---->" + (this.ticket--));
			}
		}
	}
}

public class RunnableDemo {

	public static void main(String[] args) {
		MyThread1 mt = new MyThread1();
		Thread t1 = new Thread(mt, "一号窗口");
		Thread t2 = new Thread(mt, "二号窗口");
		Thread t3 = new Thread(mt, "三号窗口");
		t1.start();
		t2.start();
		t3.start();
	}

}

// 二号窗口卖票---->10
// 三号窗口卖票---->9
// 三号窗口卖票---->7
// 一号窗口卖票---->9
// 三号窗口卖票---->6
// 二号窗口卖票---->8
// 三号窗口卖票---->4
// 一号窗口卖票---->5
// 三号窗口卖票---->2
// 二号窗口卖票---->3
// 一号窗口卖票---->1

由此差别,有同学就得出了一个结论:用Runnable就可以实现资源共享,而 Thread 不可以,这是他们的主要差别之一。。。

其实仔细看看代码就知道,这只是两种写法的区别,根本就不是 implements Runnable 与 extends Thread 的区别:

代码语言:javascript
复制
MyThread1 mt = new MyThread1();  
Thread t1 = new Thread(mt,"一号窗口");  
Thread t2 = new Thread(mt,"二号窗口");  
Thread t3 = new Thread(mt,"三号窗口"); 
////////////////
Thread t1 = new Thread(new MyThread1(),"一号窗口");  
Thread t2 = new Thread(new MyThread1(),"二号窗口");  
Thread t3 = new Thread(new MyThread1(),"三号窗口");

其实,想要“资源共享”,Thread 也可以做到的:

代码语言:javascript
复制
private static int ticket = 10;

// 三号窗口卖票---->10
// 一号窗口卖票---->9
// 二号窗口卖票---->9
// 一号窗口卖票---->7
// 一号窗口卖票---->5
// 三号窗口卖票---->8
// 一号窗口卖票---->4
// 二号窗口卖票---->6
// 一号窗口卖票---->2
// 三号窗口卖票---->3
// 二号窗口卖票---->1

通过 static 就可以实现拥有共同的ticket=10,但问题也来了,你会发现一二号窗口都卖了第 9 张票。

3、资源共享带来的问题:多线程的线程安全问题

上面的例子以及结果证明了多线程场景下,需要留意线程安全的问题:

3.1 同步run()方法

代码语言:javascript
复制
public synchronized void run()

3.2 同步 class 对象

代码语言:javascript
复制
synchronized (Test.class)

3.3 同步某些静态对象

代码语言:javascript
复制
private static final Object countLock = new Object();
synchronized (countLock) {
    count++;
}

3.4 最后给个完整的例子,模拟在线售票与查询:

代码语言:javascript
复制
package tmp;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

public class Demo implements Runnable {
	String name;
	//	static Integer tickets = 20;
	private static AtomicInteger tickets = new AtomicInteger(20);

	public Demo(String name) {
		this.name = name;
	}

	public void run() {
		for (int i = 1; i <= 20; i++) {
			synchronized (tickets) {
				if (tickets.get() > 0) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
					}
					System.out.println("我取票第" + ": " + tickets.getAndDecrement() + " 张票。");
					//					tickets--;
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
					}
					System.out.println("==========现在查询还剩" + ": " + tickets.get() + " 张票。");
				}
			}
		}
	}

	public static void main(String[] args) throws IOException {
		Demo demo = new Demo("hello");
		new Thread(demo).start();
		new Thread(demo).start();
		new Thread(demo).start();
	}
}

到这儿,本期走进科学也要跟大家说声再见了,其实聊着聊着感觉都快跑题了,多线程这块话题很多,很复杂,需要慢慢实践与积累,祝大家玩的愉快。

4、附:白话并发

办公室只有一个卫生间,一次只能容纳一个人方便,这个卫生间就是竞争条件(Race Condition)。当一个人进去后就在门口牌子上标识为“有人”,这个就相当于是线程的加锁,告诉其它同时间想要上厕所的人,这个资源已被我占位,其他人就需要等待,这叫wait。只有当前面的人出来后,并把牌子置为“无人”时,其它人才有机会使用。当只有一个蹲位时,一次只能进一个人,翻动一块牌子加一把锁,这个就叫互斥锁(Mutex)。如果卫生间里有多个蹲位,再简单地用一块牌子来标识就不行了,需要做一个电子公告牌,进去一个人电子公告牌就把可用数量减1,出来一个人数量加1,数量不为0时,有人来直接进去就行了不用等待,这个叫信号量(Semaphores)。如果出来的人是随机通知等待的某一个人,这叫notify,如果他是对着所有等待的人喊一嗓子,就是notifyAll。如果使用notify,有些倒霉的家伙可能永远也不会被通知到,这太不人性了,而如果使用nofityAll就意味着所有等待的人需要竞争资源,还是会在倒霉蛋永远轮不到。解决的办法一是按时间顺序先到先得,顺序进入,火车站的厕所经常会看到这种情况,总是有机会轮到自己,这叫公平锁(FairLock)。还有一种情况,就是大老板也在排队,一般情况下大老板时间宝贵,可以优先考虑让他先上,这叫线程优先级,一共有10个级别。优先级只能保证级别高的优先被调度到,但不能保证一定会被调度到。

两个好基友一起在蹲坑,只有一卷手纸,一个人去取时另一个就不能同时去取,这叫基于共享内存的线程间通信。两个人都是烟鬼,但只带了一个打火机,一个人用完之后递给另外一个人,

进程和线程的区别:一个办公区有多个卫生间,每个卫生间的资源是独立的,不会相互依赖,相当于是进程。每个卫生间有多个蹲位,每个蹲位相当于是一个线程,蹲位越多并发处理能力越强。但多个同一个卫生间的多个蹲位共用一个洗手台,如果蹲位过多,洗手台的资源会成为瓶颈。

Refer:

[1] Java线程面试题 Top 50

http://www.importnew.com/12773.html

[2] Difference between “implements Runnable” and “extends Thread” in java

https://www.linkedin.com/pulse/20140917120728-90925576-difference-between-implements-runnable-and-extends-thread-in-java

[3] Difference between Thread vs Runnable interface in Java

http://javarevisited.blogspot.com/2012/01/difference-thread-vs-runnable-interface.html

[4] 5 Difference Between "Implements Runnable" And "Extends Thread" In Java

http://javahungry.blogspot.com/2015/05/implements-runnable-vs-extends-thread-in-java-example.html

[5] Java中Runnable和Thread的区别

http://developer.51cto.com/art/201203/321042.htm

[6] Java中继承thread类与实现Runnable接口的区别

http://mars914.iteye.com/blog/1508429

[7] 多线程资源共享的问题

http://blog.csdn.net/king624498030/article/details/45222425

[8] Synchronized和Static Synchronized区别

http://blog.csdn.net/liovey/article/details/7456096

[9] How to synchronize a static variable among threads running different instances of a class in java?

http://stackoverflow.com/questions/2120248/how-to-synchronize-a-static-variable-among-threads-running-different-instances-o

[10] java并发和多线程

https://github.com/yikebocai/blog/blob/master/src/20130430-java_concurrency.md

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、区别:
    • 1.1 Inheritance Option:   
      • 1.2 Reusability : 
        • 1.3 Object Oriented Design:  
          • 1.4 Loosely-coupled : 
            • 1.5 Functions overhead :  
            • 2、联系:
              • 2.1 其实Thread类也是Runnable接口的子类
                • 2.2 启动线程都是 start() 方法
                  • 2.3 网传的一种缪论:用Runnable就可以实现资源共享,而 Thread 不可以
                  • 3、资源共享带来的问题:多线程的线程安全问题
                    • 3.1 同步run()方法
                      • 3.2 同步 class 对象
                        • 3.3 同步某些静态对象
                          • 3.4 最后给个完整的例子,模拟在线售票与查询:
                          • 4、附:白话并发
                          • Refer:
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档