執行緒會共享程序範圍內的資源,例如記憶體控制代碼和檔案控制代碼,但每個執行緒都有各自的程式計數器、棧及區域性變數等。執行緒還提供了一種直觀的分解模式來充分利用多處理器系統中的硬體並行性,而在同乙個程式中的多個執行緒還可以被同時排程到多個cpu上執行。執行緒也被稱為輕量級程序。在大多數現代作業系統中,都是以執行緒為基本的排程單位,而不是程序。同乙個程序中的所有執行緒都將共享程序的記憶體位址空間,因此這些執行緒都能訪問相同的成員變數並在同乙個堆上分配物件。
成員變數:
package deep;
public
class
membervariablethread
implements
runnable catch (interruptedexception e)
if (50 == i) }}
}
執行結果:package deep;
public
class test
}
thread-0:0
thread-1:0
thread-0:1
thread-1:2
thread-0:3
thread-1:4
thread-0:5
thread-1:5
thread-1:6
thread-0:6
thread-0:7
thread-1:8
thread-0:9
thread-1:10
thread-0:11
thread-1:12
thread-0:13
thread-1:14
thread-0:15
thread-1:16
thread-0:17
thread-1:18
thread-0:19
thread-1:20
thread-0:21
thread-1:22
thread-0:23
thread-1:24
thread-0:25
thread-1:26
thread-0:27
thread-1:28
thread-0:29
thread-1:30
thread-0:31
thread-1:32
thread-0:33
thread-1:34
thread-0:35
thread-1:36
thread-0:37
thread-1:38
thread-0:39
thread-1:40
thread-0:41
thread-1:42
thread-0:43
thread-1:44
thread-0:45
thread-1:46
thread-0:47
thread-1:48
thread-0:49
在此例中,i是成員變數,兩個執行緒都由同乙個類構造,所以共享了同乙個i,同時也可以觀察到此類不是執行緒安全的,因為i++不是乙個原子性操作。此例僅是為了說明成員變數會被多個執行緒共享,所以必須採取其他的機制保證執行緒安全性。
區域性變數:
package deep;
public
class
localvariablethread
implements
runnable }}
}
執行結果:package deep;
public
class test
}
thread-0:0
thread-1:0
thread-0:1
thread-1:1
thread-0:2
thread-1:2
thread-0:3
thread-1:3
thread-0:4
thread-1:4
thread-0:5
thread-1:5
thread-0:6
thread-1:6
thread-0:7
thread-1:7
thread-0:8
thread-0:9
thread-1:8
thread-0:10
thread-1:9
thread-0:11
thread-1:10
thread-1:11
thread-1:12
thread-1:13
thread-0:12
thread-1:14
thread-0:13
thread-1:15
thread-0:14
thread-1:16
thread-0:15
thread-1:17
thread-0:16
thread-1:18
thread-0:17
thread-1:19
thread-0:18
thread-1:20
thread-0:19
thread-1:21
thread-0:20
thread-1:22
thread-1:23
thread-1:24
thread-1:25
thread-1:26
thread-1:27
thread-1:28
thread-1:29
thread-1:30
thread-1:31
thread-1:32
thread-1:33
thread-0:21
thread-1:34
thread-1:35
thread-0:22
thread-1:36
thread-0:23
thread-0:24
thread-1:37
thread-0:25
thread-1:38
thread-0:26
thread-1:39
thread-0:27
thread-1:40
thread-0:28
thread-1:41
thread-0:29
thread-1:42
thread-0:30
thread-1:43
thread-0:31
thread-1:44
thread-0:32
thread-1:45
thread-0:33
thread-1:46
thread-0:34
thread-1:47
thread-0:35
thread-1:48
thread-0:36
thread-1:49
thread-0:37
thread-0:38
thread-0:39
thread-0:40
thread-0:41
thread-0:42
thread-0:43
thread-0:44
thread-0:45
thread-0:46
thread-0:47
thread-0:48
thread-0:49
如注釋中所述,由於區域性變數對於每乙個執行緒來說都有自己的拷貝,所以各個執行緒之間不再共享同乙個變數,輸出結果為100個數字,實際上是兩組,每組都是0到49的50個數字,並且兩組數字之間隨意地穿插在一起。
總結:
Java 多執行緒訪問成員變數與區域性變數
public class hellothreadtest class hellothread implements runnable catch interruptedexception e if 50 i 該例子中,hellothread類實現了runnable介面,其中run 方法的主要工作是輸...
Java 成員變數和區域性變數
成員變數 在類範圍裡定義的變數。成員變數又分為例項屬性 不用static 修飾 和類屬性 用static 修飾 類屬性和例項屬性統稱為成員變數。類屬性在類的準備階段開始存在,直到系統銷毀這個類,類屬性與類的生存範圍相同。例項屬性從類的例項被建立開始存在,直到系統銷毀這個例項,例項屬性與對應例項生存範...
成員變數與區域性變數
成員變數 作為類的成員而存在,直接存在於類中。所有類的成員變 量可以通過this來引用。區域性變數 作為方法或語句塊的成員而存在,存在於方法的引數列表和方法定義中。1.成員變數可以被 public,protect,private,static等修飾符修飾,而 區域性變數不能被控制修飾符及 stati...