java 线程 资源共享_Java 多线程之 Runnable VS Thread 以及资源共享问题

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

AAffA0nNPuCLAAAAAElFTkSuQmCC

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

1、区别:

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

1.1Inheritance 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.2Reusability:

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.3Object 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.4Loosely-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.5Functions 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接口的子类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实现的多线程。

2.3 网传的一种缪论:用Runnable就可以实现资源共享,而 Thread 不可以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 

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 代码:package tmp;class MyThread1 implements Runnable {

private int ticket = 10;

private String name;

public void run() {

for (int i = 0; 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 的区别: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 也可以做到的:private static int ticket = 10;// 三号窗口卖票---->10// 一号窗口卖票---->9// 二号窗口卖票---->9// 一号窗口卖票---->7// 一号窗口卖票---->5// 三号窗口卖票---->8// 一号窗口卖票---->4// 二号窗口卖票---->6// 一号窗口卖票---->2// 三号窗口卖票---->3// 二号窗口卖票---->1

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

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

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

3.1 同步run()方法public synchronized void run()

3.2 同步 class 对象synchronized (Test.class)

3.3 同步某些静态对象private static final Object countLock = new Object();synchronized (countLock) {    count++;}

3.4 最后给个完整的例子,模拟在线售票与查询: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();

}}

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

Refer:

[1] Java线程面试题 Top 50

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

[3] Difference between Thread vs Runnable interface in Java

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

[5] Java中Runnable和Thread的区别

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

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

[8] Synchronized和Static Synchronized区别

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

原文链接:http://outofmemory.cn/java/java.util.concurrent/runnable-vs-thread

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值