一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

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

Java 多線程編程之Thread和Runnable

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

一提到Java多線程,首先想到的是Thread繼承和Runnable的接口實現,本文就這兩個接口的實現做一個簡單的說明,有需要的朋友可以參考下

一提到Java多線程,首先想到的是Thread繼承和Runnable的接口實現

Thread繼承

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

 Runnable接口實現

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

 兩者都可以實現多線程程序的創建。實際上,我們查看Thread的代碼實現,也可以發現,Thread實際上也是實現了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 有什么區別呢?

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

執行結果輸出如下:

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片 | 波多野结衣 在线 | 农夫成人网| 精品小视频在线 | 国产目拍亚洲精品一区二区三区 | 日本动漫打扑克动画片樱花动漫 | 美女视频ww8888网网 | 色婷婷综合久久久中文字幕 | bb18lv黑料正能量 | 精品99在线观看 | 欧美日韩亚洲另类人人澡 | 97在线资源站 | 热99re久久精品国产 | 2021国产麻豆剧传媒新片 | 国产资源在线视频 | 我们日本在线观看免费动漫下载 | 亚洲gogo人体大胆西西安徽 | 久久福利影院 | 日本特级大片 | 欧美粗黑巨大gay | 69日本人xxxxxxxx色 | 3d动漫美女被吸乳羞羞视频 | xx18美女美国 | 日韩视频在线观看中字 | 久青草国产97香蕉在线视频 | 99久久伊人精品波多野结衣 | ange venus与黑人| 视频一区二区三区在线观看 | 国产一级黄色网 | 国产精品高清在线 | 久久精品国产色蜜蜜麻豆国语版 | 日本搜子同屋的日子2国语 日本爽p大片免费观看 | 亚洲美女aⅴ久久久91 | 三级理论在线观看 | 国产一级黄色录像 | 精品亚洲麻豆1区2区3区 | 亚洲国产区男人本色在线观看欧美 | 99久久国产综合精麻豆 | 四虎新网址 | 欧美8x8x | 日本成日本片人免费 |