激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語言 - JAVA教程 - Java 多線程編程之Thread和Runnable

Java 多線程編程之Thread和Runnable

2020-10-09 21:49CBDoctor JAVA教程

一提到Java多線程,首先想到的是Thread繼承和Runnable的接口實(shí)現(xiàn),本文就這兩個(gè)接口的實(shí)現(xiàn)做一個(gè)簡(jiǎn)單的說明,有需要的朋友可以參考下

一提到Java多線程,首先想到的是Thread繼承和Runnable的接口實(shí)現(xiàn)

Thread繼承

?
1
2
3
4
5
6
public class MyThread extends Thread {
    public void run(){
        int i = 0;
        System.out.println("--------------"+i++);
    }
}

 Runnable接口實(shí)現(xiàn)

?
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);
        }
    }
}

 兩者都可以實(shí)現(xiàn)多線程程序的創(chuàng)建。實(shí)際上,我們查看Thread的代碼實(shí)現(xiàn),也可以發(fā)現(xiàn),Thread實(shí)際上也是實(shí)現(xiàn)了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();
    }
 
    private char        name[];
    private int         priority;
    private Thread      threadQ;
    private long        eetop;
......
}

 那么Thread 和Runnabe 有什么區(qū)別呢?

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

?
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();
    }
}

執(zhí)行結(jié)果輸出如下:

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.

原文地址:https://www.cnblogs.com/CBDoctor/p/5077981.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 中文字幕在线观看免费视频 | 黄色一级毛片免费看 | 日韩黄色片免费看 | 欧美大穴 | 白天操夜夜操 | 久精品久久 | 成人毛片免费在线 | 国产在线一级片 | 一区二区三区欧美日韩 | 中文字幕国产一区 | 精国品产一区二区三区有限公司 | 久久久久久久久国产 | 久久福利电影网 | 草久视频在线观看 | 国产视频导航 | 一级黄色在线免费观看 | 免费a视频 | 天天黄色片 | 精品久久久久久久久久久αⅴ | 亚洲生活片| 日韩毛片一区二区三区 | 精品久久久久久国产三级 | 国内久久久久 | 在线看一级片 | 91视频观看免费 | 日韩美香港a一级毛片免费 欧美一级淫片007 | 欧美一区黄色 | 亚洲四播房 | 日韩黄色片在线观看 | av在线一区二区三区 | 久久亚洲精品视频 | 在线播放免费视频 | 91精品国产91久久久久久吃药 | 日韩视频一区二区三区四区 | 亚洲成人精品国产 | 草草影院地址 | 久久精品亚洲欧美日韩精品中文字幕 | 精品成人免费视频 | 毛片免费观看视频 | 一级毛片免费高清视频 | 国内精品视频饥渴少妇在线播放 |