国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

服務(wù)器之家 - 編程語(yǔ)言 - 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)單的說(shuō)明,有需要的朋友可以參考下

一提到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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人国产精品视频 | 国产大片在线观看 | 中文字幕高清视频 | 国产精品影视在线观看 | 91精品久久久久久久久久 | 国产精品久久久久久久一区探花 | 国产高清不卡 | 精品欧美一区二区三区久久久 | 交视频在线观看国产 | 成人在线视频一区 | 久久久久久久免费观看 | 中文字幕一区二区在线观看 | 国产精品免费自拍 | 日韩精品一区二区三区第95 | 黄色网页在线 | 亚洲电影在线观看 | 99热99| 日日干狠狠干 | 欧美中文字幕在线 | 精品视频久久久 | 久久丁香 | 日韩在线色 | 一区二区三区四区在线播放 | 欧美成人精品一区二区三区 | 欧美一级片在线观看 | 一区二区视频免费 | 1000部精品久久久久久久久 | 欧美亚洲日本 | 97碰碰碰免费公开在线视频 | 久久中文精品 | 黄色一级毛片免费看 | 国产成年免费视频 | 午夜精品久久久久久久久久久久 | 亚洲精品日本 | 蜜桃成人在线观看 | 8888色大全| 337p日本粉嫩噜噜噜 | 人人澡人人射 | 日韩电影免费在线观看中文字幕 | 国产精品美女久久久久av麻豆 | 久久精品国产视频 |