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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 九九精品热 | 热辣小秘书办公室 | 精品伊人 | 极品妖艳许清赵丽全文免费阅读 | 日韩欧美亚洲一区二区综合 | 欧美jjvideo| 好大好爽好硬我要喷水了 | 男女男精品网站免费观看 | 欧美一区二区三区久久久 | 91视频完整版 | 欧美一级艳片视频免费观看 | 天天狠天天透天干天天怕处 | 欧美亚洲第一页 | 日韩欧美一区二区三区免费看 | 4虎影视国产在线观看精品 4s4s4s4s色大众影视 | 继的朋友无遮漫画免费观看73 | 久久永久免费视频 | 视频网站入口在线看 | 精品无码国产污污污免费网站2 | 性做久久久久久久 | 国产成人8x视频一区二区 | 娇妻被朋友征服中文字幕 | 欧美久久综合网 | 国产高清亚洲 | 亚洲欧美日韩国产一区图片 | 5g影院天天 | 丁香婷婷在线视频 | 毛片段| japanesepooping脱粪| 被教官揉了一晚上的奶小说 | 秋霞啪啪片 | 狠狠色婷婷狠狠狠亚洲综合 | 欧美高清乌克兰精品另类 | 日韩亚洲人成在线 | 91大神在线精品播放 | 毛片在线网址 | 欧美一级乱妇老太婆特黄 | 亚洲国产自 | 91caoporm在线进入 | 精品福利一区 | 奇米9999 |