日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

别翻了,成员变量和局部变量在多线程中的使用,看这篇就够了

發(fā)布時間:2024/2/28 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 别翻了,成员变量和局部变量在多线程中的使用,看这篇就够了 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一. 成員變量和局部變量的區(qū)別

  • 在類中的位置不同
    成員變量:在類中方法外面
    局部變量:在方法或者代碼塊中,或者方法的聲明上(即在參數(shù)列表中)
  • 在內(nèi)存中的位置不同
    成員變量:在堆中(方法區(qū)中靜態(tài)區(qū)),成員變量屬于對象,對象進堆內(nèi)存
    局部變量:在棧中,局部變量屬于方法,方法進棧內(nèi)存
  • 生命周期不同
    成員變量:隨著對象的創(chuàng)建而存在,隨著對象的消失而消失
    局部變量:隨著方法的調(diào)用或代碼塊的執(zhí)行而存在,隨著方法的調(diào)用完畢或者代碼塊的執(zhí)行完畢而消失
  • 初始值
    成員變量:有默認(rèn)初始值
    局部變量:沒有默認(rèn)初始值,使用前需賦值
  • 注意:
    成員變量和局部變量的重名問題,就近原則;
    可以使用this關(guān)鍵字區(qū)分,this.string指的是類中的成員變量,而不是方法內(nèi)部的。
  • 代碼示例:

    public class test {public static void main(String[] args) {int a = 3; // 這個是成員變量Thread thread = new Thread(() -> {int b = 4; // 這是一個局部變量});thread.start();} }

    ?

    二. 多線程中修改調(diào)用成員變量


    當(dāng)兩個或以上線程訪問該對象的成員變量時,因為成員變量是屬于對象的,所以兩個線程共用一份成員變量,也就是說,當(dāng)一個線程對成員變量的值做出改變時,是對其他線程是有影響的。

    如果一定要使用成員變量, 可以將變量設(shè)置為static靜態(tài)值或者是AtomicInteger原子值, 但這樣會造成混亂, 以static類型為例:

    public class test {static int i = 0;public static void main(String[] args) throws Exception {Thread thread1 = new Thread(() -> {for (i = 0; i < 50; i++) {System.out.print(i + " ");}});thread1.start();Thread thread2 = new Thread(() -> {for (i = 0; i < 50; i++) {System.out.print(i + " ");}});thread2.start();} }輸出: 0 1 2 3 4 5 6 1 1 3 4 5 6 7 2 8 10 11 12 13 14 15 9 17 18 19 16 20 22 23 21 25 26 27 28 29 30 31 32 33 34 35 36 24 38 39 40 41 42 43 44 45 46 37 48 49 47

    我們發(fā)現(xiàn),本來應(yīng)該輸出兩次0-50的數(shù)字,而結(jié)果輸出是亂碼,也驗證了我們的觀點:多線程中,某線程調(diào)用并修改成員變量會對其他線程產(chǎn)生影響。

    ?

    當(dāng)然,如果我們只調(diào)用成員變量,并不修改,是可以的。 代碼如下:

    public class test {public static void main(String[] args) throws Exception {int i = 50;Thread thread1 = new Thread(() -> {for (int i1 = 0; i1 < i; i1++) {System.out.print(i1 + " ");}});thread1.start();Thread thread2 = new Thread(() -> {for (int i1 = 0; i1 < i; i1++) {System.out.print(i1 + " ");}});thread2.start();} }輸出: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 17 18 1 2 3 4 5 6 7 8 9 10 11 19 12 13 14 15 16 17 18 19 20 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 46 47 48 49 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 46 47 48 49

    可以看到,這里會輸出兩次0-50的亂序數(shù)組,是可行的。

    有沒有什么方法既可以調(diào)用并修改成員變量,又不受其他線程影響呢?

    ?
    我們可以將成員變量賦值多份,并且將其更改為static類型,或AtoimcInteger類型,這樣,多份成員變量就會存放在不同的地址里,接下來,每一個線程調(diào)用不同的成員變量即可。 以AtomicInteger類型的成員變量為例,代碼如下:

    public class test {public static void main(String[] args) throws Exception {int i = 50;AtomicInteger i1 = new AtomicInteger(i);Thread thread1 = new Thread(() -> {for (i1.set(0); i1.get() < 50; i1.getAndIncrement()) {System.out.print(i1 + " ");}});thread1.start();AtomicInteger i2 = new AtomicInteger(i);Thread thread2 = new Thread(() -> {for (i2.set(0); i2.get() < 50; i2.getAndIncrement()) {System.out.print(i2 + " ");}});thread2.start();} }結(jié)果: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 16 1 2 17 18 3 4 19 20 5 6 7 8 9 10 11 12 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 13 37 38 39 14 15 16 17 18 19 40 20 41 21 22 23 24 25 26 27 28 29 42 30 31 43 44 45 46 47 48 32 33 34 49 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

    可以看到,這里會輸出兩次0-50的亂序數(shù)組,是可行的。

    因此,可以得出結(jié)論: 如果我們想在多線程環(huán)境中,調(diào)用并修改成員變量,需要把成員變量復(fù)制多份,更改為static類型,或AtoimcInteger類型, 調(diào)用即可。

    ?

    三. 多線程中修改調(diào)用局部變量


    由于每個局部變量對于每個線程來說,都是私有的,因此直接調(diào)用修改即可,并不會影響到其他線程。 代碼如下:

    public class test {public static void main(String[] args) throws Exception {Thread thread1 = new Thread(() -> {int i = 0;for (i = 0; i < 50; i++) {System.out.println(i + " ");}});thread1.start();Thread thread2 = new Thread(() -> {int i = 0;for (i = 0; i < 50; i++) {System.out.println(i + " ");}});thread2.start();} }輸出: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 0 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 23 44 24 25 45 46 47 26 48 49 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

    可以看到,這里會輸出兩次0-50的亂序數(shù)組,是可行的。

    總結(jié)

    以上是生活随笔為你收集整理的别翻了,成员变量和局部变量在多线程中的使用,看这篇就够了的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。