`

ava.util.concurrent源码学习系列--volatile语义

阅读更多

volatile语义:告诉处理器,不要到工作内存中找我,而是直接到主存中操作我,多线程或者多核环境下,变量共享使用volatile要注意,他只能保证可见性,但不能保证原子性;

如i++之类的操作,他分为read i的值,之后执行i+1
当出现并发情况时,1线程read i的值,而2线程修改了i的值,这个时候1线程如果再将值刷到主存的话就会造成覆盖。可以通过synchronized在同步代码段,保证原子性或者使用jdk1.5的原子包

参考:

java中volatile关键字的含义

参考中的例子说服力不是很够,这里重写了参考的例子,使得其更有说服力。

 

public class Counter {
	public volatile static int count = 0;
	
	public static void inc(){
		
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
			//TODO
			}
			count++;
		
	}
	
	public static void main(String[] args){
		
		for(int i=0;i<1000;i++){
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					Counter.inc();
					System.out.println(Thread.currentThread().getName());
					
				}
			}).start();
		}
		try {
			Thread.currentThread().sleep(10000);
		} catch (InterruptedException e) {
			//TODO
		}
		System.out.println("运行结果:Counter.count=" + Counter.count);
		System.out.println(Thread.currentThread().getName());
	}

}

 输出结果如下:

 

Thread-14
Thread-10
Thread-5
Thread-1
Thread-9
Thread-17
Thread-2
Thread-11
Thread-4
Thread-6
Thread-12
Thread-25
Thread-29
Thread-22
Thread-7
Thread-23
Thread-15
Thread-37
Thread-36
Thread-8
Thread-3
Thread-13
Thread-33
Thread-41
Thread-32
Thread-28
Thread-42
Thread-26
Thread-18
Thread-21
Thread-45
Thread-19
Thread-53
Thread-27
Thread-46
Thread-50
Thread-0
Thread-31
Thread-69
Thread-57
Thread-58
Thread-49
Thread-40
Thread-24
Thread-20
Thread-16
Thread-38
Thread-34
Thread-30
Thread-91
Thread-76
Thread-90
Thread-88
Thread-92
Thread-61
Thread-79
Thread-86
Thread-52
Thread-94
Thread-93
Thread-72
Thread-64
Thread-89
Thread-85
Thread-60
Thread-66
Thread-44
Thread-82
Thread-67
Thread-68
Thread-63
Thread-73
Thread-55
Thread-70
Thread-80
Thread-97
Thread-35
Thread-71
Thread-47
Thread-74
Thread-39
Thread-75
Thread-78
Thread-77
Thread-83
Thread-62
Thread-104
Thread-95
Thread-87
Thread-81
Thread-48
Thread-56
Thread-112
Thread-43
Thread-65
Thread-54
Thread-100
Thread-84
Thread-98
Thread-51
Thread-59
Thread-101
Thread-129
Thread-133
Thread-140
Thread-136
Thread-138
Thread-130
Thread-134
Thread-131
Thread-125
Thread-122
Thread-103
Thread-114
Thread-110
Thread-102
Thread-113
Thread-99
Thread-108
Thread-145
Thread-119
Thread-96
Thread-116
Thread-120
Thread-144
Thread-142
Thread-105
Thread-109
Thread-118
Thread-106
Thread-124
Thread-128
Thread-132
Thread-115
Thread-117
Thread-121
Thread-127
Thread-107
Thread-126
Thread-111
Thread-135
Thread-123
Thread-141
Thread-160
Thread-166
Thread-137
Thread-155
Thread-161
Thread-147
Thread-156
Thread-164
Thread-165
Thread-157
Thread-151
Thread-148
Thread-152
Thread-167
Thread-158
Thread-169
Thread-173
Thread-150
Thread-146
Thread-154
Thread-153
Thread-149
Thread-174
Thread-139
Thread-143
Thread-172
Thread-170
Thread-163
Thread-177
Thread-181
Thread-185
Thread-182
Thread-159
Thread-186
Thread-162
Thread-175
Thread-168
Thread-171
Thread-179
Thread-176
Thread-189
Thread-188
Thread-190
Thread-184
Thread-180
Thread-194
Thread-178
Thread-193
Thread-183
Thread-187
Thread-192
Thread-197
Thread-196
Thread-198
Thread-191
Thread-200
Thread-195
Thread-199
Thread-206
Thread-201
Thread-209
Thread-203
Thread-204
Thread-208
Thread-207
Thread-205
Thread-202
Thread-210
Thread-216
Thread-215
Thread-220
Thread-212
Thread-214
Thread-219
Thread-217
Thread-213
Thread-211
Thread-225
Thread-222
Thread-218
Thread-223
Thread-221
Thread-229
Thread-227
Thread-224
Thread-230
Thread-226
Thread-231
Thread-228
Thread-241
Thread-233
Thread-237
Thread-238
Thread-236
Thread-234
Thread-240
Thread-232
Thread-235
Thread-252
Thread-245
Thread-244
Thread-242
Thread-243
Thread-249
Thread-250
Thread-246
Thread-248
Thread-247
Thread-239
Thread-251
Thread-256
Thread-254
Thread-258
Thread-255
Thread-257
Thread-253
Thread-264
Thread-259
Thread-268
Thread-260
Thread-270
Thread-266
Thread-262
Thread-265
Thread-261
Thread-277
Thread-273
Thread-269
Thread-275
Thread-267
Thread-281
Thread-282
Thread-285
Thread-286
Thread-272
Thread-295
Thread-314
Thread-299
Thread-310
Thread-303
Thread-307
Thread-312
Thread-308
Thread-287
Thread-309
Thread-301
Thread-302
Thread-297
Thread-296
Thread-306
Thread-288
Thread-274
Thread-300
Thread-292
Thread-271
Thread-284
Thread-304
Thread-280
Thread-263
Thread-276
Thread-289
Thread-278
Thread-290
Thread-294
Thread-298
Thread-279
Thread-315
Thread-311
Thread-316
Thread-293
Thread-283
Thread-318
Thread-291
Thread-305
Thread-317
Thread-313
Thread-325
Thread-321
Thread-319
Thread-326
Thread-323
Thread-322
Thread-327
Thread-329
Thread-333
Thread-337
Thread-331
Thread-320
Thread-324
Thread-339
Thread-338
Thread-328
Thread-343
Thread-330
Thread-332
Thread-334
Thread-342
Thread-346
Thread-335
Thread-345
Thread-349
Thread-341
Thread-350
Thread-347
Thread-351
Thread-354
Thread-348
Thread-344
Thread-336
Thread-352
Thread-362
Thread-355
Thread-363
Thread-340
Thread-356
Thread-353
Thread-359
Thread-358
Thread-357
Thread-367
Thread-366
Thread-368
Thread-364
Thread-360
Thread-372
Thread-371
Thread-381
Thread-385
Thread-377
Thread-384
Thread-380
Thread-374
Thread-370
Thread-382
Thread-378
Thread-375
Thread-388
Thread-386
Thread-379
Thread-376
Thread-390
Thread-361
Thread-365
Thread-373
Thread-392
Thread-391
Thread-387
Thread-383
Thread-369
Thread-396
Thread-395
Thread-393
Thread-394
Thread-389
Thread-398
Thread-400
Thread-399
Thread-402
Thread-406
Thread-410
Thread-409
Thread-405
Thread-408
Thread-401
Thread-404
Thread-407
Thread-414
Thread-403
Thread-420
Thread-416
Thread-412
Thread-427
Thread-443
Thread-439
Thread-426
Thread-432
Thread-442
Thread-450
Thread-424
Thread-428
Thread-451
Thread-397
Thread-455
Thread-454
Thread-413
Thread-417
Thread-452
Thread-448
Thread-436
Thread-446
Thread-433
Thread-437
Thread-456
Thread-447
Thread-438
Thread-434
Thread-430
Thread-444
Thread-440
Thread-422
Thread-423
Thread-435
Thread-449
Thread-466
Thread-431
Thread-457
Thread-415
Thread-411
Thread-419
Thread-418
Thread-429
Thread-469
Thread-477
Thread-479
Thread-445
Thread-470
Thread-453
Thread-462
Thread-478
Thread-476
Thread-459
Thread-441
Thread-458
Thread-464
Thread-463
Thread-425
Thread-482
Thread-481
Thread-490
Thread-475
Thread-483
Thread-474
Thread-486
Thread-421
Thread-473
Thread-472
Thread-471
Thread-468
Thread-467
Thread-501
Thread-506
Thread-500
Thread-496
Thread-488
Thread-504
Thread-485
Thread-460
Thread-497
Thread-492
Thread-493
Thread-491
Thread-502
Thread-498
Thread-507
Thread-513
Thread-487
Thread-494
Thread-484
Thread-489
Thread-480
Thread-465
Thread-511
Thread-461
Thread-508
Thread-518
Thread-517
Thread-520
Thread-512
Thread-516
Thread-509
Thread-514
Thread-510
Thread-503
Thread-499
Thread-505
Thread-495
Thread-515
Thread-522
Thread-519
Thread-521
Thread-523
Thread-532
Thread-525
Thread-528
Thread-535
Thread-526
Thread-530
Thread-527
Thread-534
Thread-533
Thread-531
Thread-540
Thread-529
Thread-536
Thread-537
Thread-524
Thread-546
Thread-538
Thread-544
Thread-542
Thread-541
Thread-539
Thread-543
Thread-550
Thread-554
Thread-549
Thread-551
Thread-557
Thread-555
Thread-560
Thread-552
Thread-553
Thread-547
Thread-545
Thread-548
Thread-559
Thread-556
Thread-558
Thread-561
Thread-562
Thread-565
Thread-564
Thread-569
Thread-563
Thread-570
Thread-568
Thread-566
Thread-577
Thread-579
Thread-572
Thread-567
Thread-571
Thread-573
Thread-578
Thread-576
Thread-574
Thread-590
Thread-585
Thread-584
Thread-582
Thread-581
Thread-580
Thread-586
Thread-583
Thread-588
Thread-575
Thread-594
Thread-593
Thread-597
Thread-589
Thread-598
Thread-592
Thread-591
Thread-587
Thread-599
Thread-603
Thread-602
Thread-606
Thread-607
Thread-600
Thread-604
Thread-605
Thread-596
Thread-601
Thread-595
Thread-616
Thread-608
Thread-613
Thread-610
Thread-609
Thread-612
Thread-614
Thread-611
Thread-617
Thread-624
Thread-626
Thread-620
Thread-625
Thread-618
Thread-621
Thread-622
Thread-615
Thread-628
Thread-629
Thread-619
Thread-623
Thread-627
Thread-638
Thread-631
Thread-635
Thread-630
Thread-634
Thread-632
Thread-640
Thread-643
Thread-647
Thread-636
Thread-639
Thread-642
Thread-651
Thread-652
Thread-644
Thread-648
Thread-650
Thread-646
Thread-633
Thread-637
Thread-649
Thread-660
Thread-658
Thread-662
Thread-655
Thread-657
Thread-645
Thread-641
Thread-661
Thread-653
Thread-654
Thread-656
Thread-669
Thread-670
Thread-666
Thread-668
Thread-665
Thread-672
Thread-664
Thread-674
Thread-678
Thread-673
Thread-676
Thread-663
Thread-667
Thread-677
Thread-671
Thread-659
Thread-682
Thread-679
Thread-684
Thread-685
Thread-680
Thread-681
Thread-675
Thread-686
Thread-683
Thread-687
Thread-688
Thread-691
Thread-689
Thread-700
Thread-692
Thread-702
Thread-695
Thread-690
Thread-694
Thread-701
Thread-698
Thread-699
Thread-697
Thread-693
Thread-696
Thread-708
Thread-709
Thread-710
Thread-706
Thread-704
Thread-705
Thread-707
Thread-703
Thread-712
Thread-718
Thread-722
Thread-714
Thread-713
Thread-711
Thread-716
Thread-715
Thread-717
Thread-720
Thread-721
Thread-730
Thread-726
Thread-719
Thread-728
Thread-723
Thread-731
Thread-727
Thread-725
Thread-737
Thread-729
Thread-733
Thread-734
Thread-724
Thread-732
Thread-736
Thread-738
Thread-735
Thread-741
Thread-745
Thread-749
Thread-753
Thread-761
Thread-758
Thread-742
Thread-754
Thread-765
Thread-762
Thread-746
Thread-773
Thread-769
Thread-766
Thread-770
Thread-740
Thread-739
Thread-743
Thread-747
Thread-777
Thread-750
Thread-755
Thread-763
Thread-744
Thread-748
Thread-774
Thread-757
Thread-782
Thread-789
Thread-751
Thread-790
Thread-786
Thread-785
Thread-767
Thread-794
Thread-781
Thread-778
Thread-756
Thread-764
Thread-791
Thread-795
Thread-798
Thread-793
Thread-775
Thread-760
Thread-752
Thread-768
Thread-779
Thread-802
Thread-783
Thread-814
Thread-817
Thread-813
Thread-807
Thread-772
Thread-787
Thread-800
Thread-788
Thread-759
Thread-816
Thread-784
Thread-792
Thread-796
Thread-819
Thread-818
Thread-771
Thread-801
Thread-804
Thread-812
Thread-806
Thread-820
Thread-808
Thread-810
Thread-776
Thread-780
Thread-826
Thread-821
Thread-805
Thread-809
Thread-827
Thread-825
Thread-797
Thread-828
Thread-832
Thread-834
Thread-830
Thread-838
Thread-841
Thread-845
Thread-849
Thread-842
Thread-853
Thread-846
Thread-850
Thread-854
Thread-852
Thread-857
Thread-861
Thread-865
Thread-862
Thread-815
Thread-866
Thread-811
Thread-803
Thread-822
Thread-799
Thread-856
Thread-848
Thread-844
Thread-870
Thread-864
Thread-874
Thread-831
Thread-835
Thread-863
Thread-867
Thread-871
Thread-868
Thread-887
Thread-891
Thread-840
Thread-875
Thread-883
Thread-895
Thread-837
Thread-880
Thread-836
Thread-824
Thread-829
Thread-890
Thread-869
Thread-906
Thread-910
Thread-833
Thread-873
Thread-823
Thread-860
Thread-872
Thread-877
Thread-904
Thread-899
Thread-876
Thread-908
Thread-897
Thread-905
Thread-843
Thread-885
Thread-847
Thread-859
Thread-901
Thread-881
Thread-839
Thread-894
Thread-879
Thread-888
Thread-878
Thread-912
Thread-886
Thread-882
Thread-851
Thread-914
Thread-903
Thread-900
Thread-896
Thread-892
Thread-924
Thread-909
Thread-884
Thread-922
Thread-925
Thread-902
Thread-898
Thread-916
Thread-920
Thread-911
Thread-915
Thread-928
Thread-926
Thread-927
Thread-923
Thread-931
Thread-932
Thread-913
Thread-936
Thread-855
Thread-907
Thread-934
Thread-950
Thread-946
Thread-942
Thread-938
Thread-951
Thread-944
Thread-940
Thread-952
Thread-937
Thread-941
Thread-943
Thread-935
Thread-948
Thread-939
Thread-947
Thread-930
Thread-919
Thread-921
Thread-917
Thread-893
Thread-918
Thread-889
Thread-858
Thread-953
Thread-954
Thread-945
Thread-933
Thread-929
Thread-949
Thread-958
Thread-962
Thread-964
Thread-961
Thread-959
Thread-960
Thread-957
Thread-956
Thread-955
Thread-963
Thread-972
Thread-965
Thread-969
Thread-973
Thread-967
Thread-970
Thread-966
Thread-968
Thread-971
Thread-974
Thread-975
Thread-982
Thread-976
Thread-984
Thread-980
Thread-979
Thread-983
Thread-977
Thread-978
Thread-981
Thread-991
Thread-992
Thread-993
Thread-988
Thread-989
Thread-994
Thread-987
Thread-985
Thread-986
Thread-990
Thread-998
Thread-996
Thread-995
Thread-997
Thread-999
运行结果:Counter.count=993
main                                                                                                                                                                    

如果加了public class Counter {
	public volatile static int count = 0;
	
	public synchronized static void inc(){
		
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
			//TODO
			}
			count++;
		
	}
	
	public static void main(String[] args){
		
		for(int i=0;i<1000;i++){
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					Counter.inc();
					System.out.println(Thread.currentThread().getName());
					
				}
			}).start();
		}
		try {
			Thread.currentThread().sleep(10000);
		} catch (InterruptedException e) {
			//TODO
		}
		System.out.println("运行结果:Counter.count=" + Counter.count);
		System.out.println(Thread.currentThread().getName());
	}

}

 如果加了synchronized关键字后

public class Counter {
	public volatile static int count = 0;
	
	public synchronized static void inc(){
		
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
			//TODO
			}
			count++;
		
	}
	
	public static void main(String[] args){
		
		for(int i=0;i<1000;i++){
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					Counter.inc();
					System.out.println(Thread.currentThread().getName());
					
				}
			}).start();
		}
		try {
			Thread.currentThread().sleep(10000);
		} catch (InterruptedException e) {
			//TODO
		}
		System.out.println("运行结果:Counter.count=" + Counter.count);
		System.out.println(Thread.currentThread().getName());
	}

}
 输出结果为:
Thread-0
Thread-5
Thread-4
Thread-3
Thread-2
Thread-1
Thread-59
Thread-58
Thread-52
Thread-98
Thread-54
Thread-56
Thread-140
Thread-55
Thread-51
Thread-47
Thread-43
Thread-39
Thread-35
Thread-50
Thread-234
Thread-48
Thread-46
Thread-275
Thread-42
Thread-38
Thread-44
Thread-342
Thread-40
Thread-36
Thread-45
Thread-41
Thread-420
Thread-37
Thread-452
Thread-33
Thread-34
Thread-32
Thread-31
Thread-30
Thread-29
Thread-28
Thread-571
Thread-27
Thread-26
Thread-25
Thread-24
Thread-23
Thread-658
Thread-22
Thread-677
Thread-21
Thread-20
Thread-684
Thread-19
Thread-17
Thread-18
Thread-16
Thread-15
Thread-14
Thread-13
Thread-782
Thread-12
Thread-11
Thread-10
Thread-9
Thread-8
Thread-7
Thread-6
Thread-895
Thread-893
Thread-892
Thread-891
Thread-890
Thread-974
Thread-889
Thread-888
Thread-887
Thread-886
Thread-885
Thread-884
Thread-883
Thread-882
Thread-881
Thread-880
Thread-879
Thread-878
Thread-877
Thread-876
Thread-875
Thread-874
Thread-873
Thread-872
Thread-871
Thread-870
Thread-869
Thread-868
Thread-867
Thread-866
Thread-865
Thread-864
Thread-863
Thread-862
Thread-861
Thread-860
Thread-859
Thread-858
Thread-857
Thread-856
Thread-855
Thread-854
Thread-853
Thread-852
Thread-851
Thread-850
Thread-849
Thread-848
Thread-847
Thread-846
Thread-845
Thread-844
Thread-843
Thread-842
Thread-841
Thread-840
Thread-839
Thread-838
Thread-837
Thread-836
Thread-835
Thread-834
Thread-833
Thread-832
Thread-831
Thread-830
Thread-829
Thread-828
Thread-827
Thread-826
Thread-825
Thread-824
Thread-823
Thread-822
Thread-821
Thread-820
Thread-819
Thread-818
Thread-817
Thread-816
Thread-815
Thread-814
Thread-813
Thread-812
Thread-811
Thread-810
Thread-809
Thread-808
Thread-807
Thread-806
Thread-805
Thread-804
Thread-803
Thread-802
Thread-801
Thread-800
Thread-799
Thread-798
Thread-797
Thread-796
Thread-795
Thread-794
Thread-793
Thread-792
Thread-791
Thread-790
Thread-789
Thread-788
Thread-787
Thread-786
Thread-785
Thread-784
Thread-783
Thread-781
Thread-780
Thread-779
Thread-778
Thread-777
Thread-776
Thread-775
Thread-774
Thread-773
Thread-772
Thread-771
Thread-770
Thread-769
Thread-768
Thread-767
Thread-766
Thread-765
Thread-763
Thread-764
Thread-762
Thread-761
Thread-760
Thread-759
Thread-758
Thread-757
Thread-756
Thread-755
Thread-754
Thread-753
Thread-752
Thread-751
Thread-750
Thread-749
Thread-748
Thread-747
Thread-746
Thread-745
Thread-744
Thread-743
Thread-742
Thread-741
Thread-740
Thread-739
Thread-738
Thread-737
Thread-736
Thread-735
Thread-734
Thread-733
Thread-732
Thread-731
Thread-730
Thread-729
Thread-727
Thread-728
Thread-726
Thread-725
Thread-724
Thread-723
Thread-719
Thread-722
Thread-721
Thread-720
Thread-717
Thread-718
Thread-716
Thread-715
Thread-714
Thread-713
Thread-712
Thread-711
Thread-710
Thread-709
Thread-708
Thread-707
Thread-706
Thread-705
Thread-704
Thread-703
Thread-702
Thread-701
Thread-700
Thread-699
Thread-698
Thread-697
Thread-696
Thread-695
Thread-694
Thread-693
Thread-692
Thread-691
Thread-690
Thread-689
Thread-688
Thread-687
Thread-686
Thread-685
Thread-683
Thread-682
Thread-681
Thread-680
Thread-679
Thread-675
Thread-678
Thread-676
Thread-674
Thread-673
Thread-672
Thread-671
Thread-670
Thread-666
Thread-668
Thread-669
Thread-667
Thread-665
Thread-664
Thread-663
Thread-659
Thread-655
Thread-662
Thread-661
Thread-660
Thread-657
Thread-653
Thread-656
Thread-651
Thread-654
Thread-652
Thread-650
Thread-649
Thread-648
Thread-645
Thread-646
Thread-647
Thread-644
Thread-643
Thread-641
Thread-642
Thread-640
Thread-639
Thread-638
Thread-637
Thread-636
Thread-635
Thread-634
Thread-633
Thread-632
Thread-631
Thread-630
Thread-629
Thread-628
Thread-627
Thread-626
Thread-625
Thread-624
Thread-623
Thread-622
Thread-621
Thread-620
Thread-619
Thread-618
Thread-617
Thread-616
Thread-615
Thread-614
Thread-613
Thread-612
Thread-611
Thread-610
Thread-609
Thread-608
Thread-607
Thread-606
Thread-605
Thread-604
Thread-603
Thread-602
Thread-601
Thread-600
Thread-599
Thread-598
Thread-597
Thread-596
Thread-595
Thread-594
Thread-593
Thread-592
Thread-591
Thread-590
Thread-589
Thread-588
Thread-587
Thread-586
Thread-585
Thread-584
Thread-583
Thread-582
Thread-581
Thread-580
Thread-579
Thread-578
Thread-577
Thread-576
Thread-575
Thread-574
Thread-573
Thread-572
Thread-570
Thread-569
Thread-568
Thread-567
Thread-566
Thread-565
Thread-564
Thread-563
Thread-562
Thread-561
Thread-560
Thread-559
Thread-558
Thread-557
Thread-556
Thread-555
Thread-554
Thread-553
Thread-552
Thread-551
Thread-550
Thread-549
Thread-548
Thread-547
Thread-546
Thread-545
Thread-544
Thread-543
Thread-542
Thread-541
Thread-540
Thread-539
Thread-538
Thread-537
Thread-536
Thread-535
Thread-534
Thread-533
Thread-532
Thread-531
Thread-530
Thread-529
Thread-528
Thread-527
Thread-526
Thread-524
Thread-525
Thread-523
Thread-522
Thread-521
Thread-520
Thread-519
Thread-518
Thread-517
Thread-516
Thread-515
Thread-514
Thread-513
Thread-512
Thread-511
Thread-510
Thread-509
Thread-508
Thread-507
Thread-506
Thread-505
Thread-504
Thread-503
Thread-502
Thread-501
Thread-500
Thread-499
Thread-498
Thread-497
Thread-496
Thread-495
Thread-494
Thread-493
Thread-492
Thread-491
Thread-490
Thread-487
Thread-489
Thread-488
Thread-486
Thread-485
Thread-484
Thread-483
Thread-482
Thread-481
Thread-480
Thread-479
Thread-478
Thread-477
Thread-476
Thread-475
Thread-474
Thread-473
Thread-472
Thread-471
Thread-470
Thread-469
Thread-468
Thread-467
Thread-466
Thread-465
Thread-464
Thread-463
Thread-462
Thread-461
Thread-460
Thread-459
Thread-458
Thread-457
Thread-456
Thread-455
Thread-454
Thread-453
Thread-451
Thread-450
Thread-449
Thread-448
Thread-447
Thread-446
Thread-445
Thread-444
Thread-443
Thread-442
Thread-441
Thread-440
Thread-439
Thread-438
Thread-437
Thread-436
Thread-435
Thread-434
Thread-433
Thread-432
Thread-431
Thread-430
Thread-429
Thread-428
Thread-427
Thread-426
Thread-425
Thread-424
Thread-423
Thread-421
Thread-422
Thread-419
Thread-418
Thread-417
Thread-416
Thread-415
Thread-414
Thread-413
Thread-412
Thread-411
Thread-410
Thread-408
Thread-409
Thread-407
Thread-406
Thread-405
Thread-404
Thread-403
Thread-402
Thread-401
Thread-400
Thread-399
Thread-398
Thread-397
Thread-396
Thread-395
Thread-394
Thread-393
Thread-392
Thread-391
Thread-390
Thread-389
Thread-388
Thread-387
Thread-386
Thread-385
Thread-384
Thread-383
Thread-382
Thread-381
Thread-380
Thread-379
Thread-378
Thread-377
Thread-376
Thread-375
Thread-374
Thread-373
Thread-372
Thread-371
Thread-370
Thread-369
Thread-368
Thread-367
Thread-366
Thread-365
Thread-364
Thread-363
Thread-362
Thread-361
Thread-360
Thread-359
Thread-358
Thread-357
Thread-356
Thread-355
Thread-354
Thread-353
Thread-352
Thread-351
Thread-350
Thread-349
Thread-348
Thread-347
Thread-346
Thread-345
Thread-344
Thread-343
Thread-341
Thread-340
Thread-339
Thread-338
Thread-337
Thread-336
Thread-335
Thread-334
Thread-333
Thread-332
Thread-331
Thread-330
Thread-329
Thread-328
Thread-327
Thread-326
Thread-325
Thread-324
Thread-323
Thread-322
Thread-321
Thread-320
Thread-319
Thread-318
Thread-317
Thread-316
Thread-315
Thread-314
Thread-313
Thread-312
Thread-311
Thread-310
Thread-309
Thread-308
Thread-307
Thread-306
Thread-305
Thread-304
Thread-303
Thread-302
Thread-301
Thread-300
Thread-299
Thread-298
Thread-297
Thread-296
Thread-295
Thread-294
Thread-293
Thread-292
Thread-291
Thread-290
Thread-289
Thread-288
Thread-287
Thread-286
Thread-285
Thread-284
Thread-283
Thread-282
Thread-281
Thread-280
Thread-279
Thread-278
Thread-277
Thread-276
Thread-274
Thread-273
Thread-272
Thread-271
Thread-270
Thread-269
Thread-268
Thread-267
Thread-266
Thread-265
Thread-264
Thread-263
Thread-262
Thread-261
Thread-260
Thread-259
Thread-258
Thread-257
Thread-256
Thread-255
Thread-254
Thread-253
Thread-252
Thread-251
Thread-250
Thread-249
Thread-248
Thread-247
Thread-246
Thread-245
Thread-244
Thread-243
Thread-242
Thread-241
Thread-240
Thread-239
Thread-238
Thread-237
Thread-236
Thread-235
Thread-233
Thread-232
Thread-231
Thread-230
Thread-229
Thread-228
Thread-227
Thread-226
Thread-225
Thread-224
Thread-223
Thread-222
Thread-221
Thread-220
Thread-219
Thread-218
Thread-217
Thread-216
Thread-213
Thread-215
Thread-214
Thread-211
Thread-212
Thread-209
Thread-210
Thread-208
Thread-205
Thread-206
Thread-207
Thread-204
Thread-201
Thread-203
Thread-202
Thread-197
Thread-200
Thread-199
Thread-198
Thread-195
Thread-196
Thread-193
Thread-194
Thread-192
Thread-191
Thread-190
Thread-189
Thread-188
Thread-187
Thread-186
Thread-185
Thread-184
Thread-183
Thread-182
Thread-181
Thread-180
Thread-179
Thread-178
Thread-177
Thread-176
Thread-175
Thread-174
Thread-173
Thread-171
Thread-172
Thread-170
Thread-169
Thread-168
Thread-167
Thread-166
Thread-165
Thread-164
Thread-163
Thread-162
Thread-161
Thread-160
Thread-159
Thread-158
Thread-157
Thread-156
Thread-155
Thread-154
Thread-153
Thread-152
Thread-151
Thread-150
Thread-149
Thread-148
Thread-147
Thread-146
Thread-145
Thread-144
Thread-143
Thread-142
Thread-141
Thread-139
Thread-138
Thread-137
Thread-136
Thread-135
Thread-134
Thread-133
Thread-132
Thread-131
Thread-130
Thread-129
Thread-128
Thread-127
Thread-126
Thread-125
Thread-124
Thread-123
Thread-122
Thread-121
Thread-120
Thread-119
Thread-118
Thread-117
Thread-116
Thread-115
Thread-114
Thread-113
Thread-112
Thread-111
Thread-110
Thread-109
Thread-108
Thread-107
Thread-106
Thread-104
Thread-105
Thread-103
Thread-102
Thread-101
Thread-100
Thread-99
Thread-97
Thread-96
Thread-95
Thread-94
Thread-93
Thread-92
Thread-91
Thread-90
Thread-89
Thread-88
Thread-87
Thread-86
Thread-85
Thread-84
Thread-83
Thread-82
Thread-81
Thread-80
Thread-79
Thread-78
Thread-77
Thread-75
Thread-76
Thread-74
Thread-73
Thread-72
Thread-71
Thread-70
Thread-69
Thread-68
Thread-66
Thread-67
Thread-65
Thread-61
Thread-57
Thread-53
Thread-64
Thread-63
Thread-49
Thread-62
Thread-60
Thread-999
Thread-998
Thread-997
Thread-996
Thread-995
Thread-994
Thread-993
Thread-992
Thread-991
Thread-990
Thread-989
Thread-988
Thread-987
Thread-986
Thread-985
Thread-984
Thread-983
Thread-982
Thread-981
Thread-980
Thread-979
Thread-978
Thread-977
Thread-976
Thread-975
Thread-973
Thread-972
Thread-971
Thread-970
Thread-969
Thread-968
Thread-967
Thread-966
Thread-965
Thread-964
Thread-963
Thread-962
Thread-961
Thread-960
Thread-959
Thread-958
Thread-957
Thread-956
Thread-955
Thread-954
Thread-953
Thread-952
Thread-951
Thread-950
Thread-949
Thread-948
Thread-947
Thread-946
Thread-945
Thread-944
Thread-943
Thread-942
Thread-941
Thread-940
Thread-939
Thread-938
Thread-937
Thread-936
Thread-935
Thread-934
Thread-933
Thread-932
Thread-931
Thread-930
Thread-929
Thread-928
Thread-927
Thread-926
Thread-925
Thread-924
Thread-923
Thread-922
Thread-921
Thread-920
Thread-919
Thread-918
Thread-917
Thread-916
Thread-915
Thread-914
Thread-913
Thread-912
Thread-911
Thread-910
Thread-909
Thread-908
Thread-907
Thread-906
Thread-905
Thread-904
Thread-903
Thread-902
Thread-901
Thread-900
Thread-899
Thread-898
Thread-897
Thread-896
Thread-894
运行结果:Counter.count=1000
main
结果和预期的一样,说明volatile不能达到原子操作。
分享到:
评论

相关推荐

    java并发工具包 java.util.concurrent中文版用户指南pdf

    1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...

    atlassian-util-concurrent-0.0.12.jar.zip

    `atlassian-util-concurrent-0.0.12.jar.zip`是这个库的压缩文件,包含了`atlassian-util-concurrent-0.0.12.jar`本身以及相关的许可证文件`atlassian-profiling-license.txt`。这个库是Atlassian为Java开发者提供的...

    Tomcat内存溢出的解决方法(java.util.concurrent.ExecutionException)

    "java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError" 是一个典型的错误提示,它表明在并发执行过程中遇到了内存不足的问题。下面我们将深入探讨这个问题的原因、影响以及如何解决。 内存溢出...

    java.util.concurrent 学习ppt

    Java.util.concurrent的引入是为了解决传统并发原语如wait()、notify()、synchronized和volatile的使用难度大、容易出错以及性能问题。 在并发编程中,我们经常会遇到需要管理多个线程执行任务的情况。传统的做法是...

    backport-util-concurrent(2.2 /3.1)

    《backport-util-concurrent:Java并发编程的利器》 在Java的世界里,高效并发处理是提升应用程序性能的关键因素之一。backport-util-concurrent库,正如其名,是一种将Java 5及以上版本的并发特性“回移植”到Java...

    java.util.concurrent

    java.util.concurrent总体概览图。 收取资源分3分。需要的同学可以下载一下。 java.util.concurrent主要包括5个部分executor,colletions,locks,atomic,tools。 该图详细的列举了并发包下面的结构,包含所有接口和...

    java并发工具包 java.util.concurrent中文版pdf

    为了简化并发编程的复杂性,Java 5 引入了 `java.util.concurrent` 包,提供了一系列强大的类和接口,帮助开发者更高效地管理多线程任务。 #### 二、`java.util.concurrent` 概览 `java.util.concurrent` 包提供了...

    asm-util.jar

    asm-util-1.3.4.jar, asm-util-1.3.5.jar, asm-util-1.4.1.jar, asm-util-1.4.3.jar, asm-util-1.5.1.jar, asm-util-1.5.2.jar, asm-util-1.5.3.jar, asm-util-2.0.jar, asm-util-2.1.jar, asm-util-2.2.1-sources....

    Java并发工具包java.util.concurrent用户指南中英文对照阅读版.pdf

    java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...

    java.util.concurrent 测试源文件

    Java.util.concurrent(JUC)是Java平台中的一个核心包,专门用于处理多线程并发问题。这个包包含了大量的工具类和接口,极大地简化了并发编程的复杂性,提高了程序的性能和可伸缩性。本测试源文件主要是针对JUC并发...

    JDK1.5中的线程池(java.util.concurrent.ThreadPoolExecutor)使用

    "JDK1.5中的线程池(java.util.concurrent.ThreadPoolExecutor)使用" JDK1.5中的线程池(java.util.concurrent.ThreadPoolExecutor)使用是Java多线程编程中的一种重要概念。随着多线程编程的普及,线程池的使用变得...

    The java.util.concurrent Synchronizer Framework

    ### Java.util.concurrent.Synchronizer框架详解 #### 一、引言与背景 随着Java技术的发展,多线程编程成为了一项重要的技术需求。为了更好地支持并发编程,Java平台在J2SE 1.5版本中引入了`java.util.concurrent`...

    backport-util-concurrent.jar

    backport-util-concurrent.jarbackport-util-concurrent.jarbackport-util-concurrent.jar

    java.util.concurrent-多线程框架.docx

    java.util.concurrent 多线程框架 java.util.concurrent 多线程框架是 Java 语言中用于多线程编程的库。该库提供了多种线程池实现、并发集合、同步器、lock 等多种机制,以便开发者更方便地编写高效、可靠的多线程...

    动画学习 java.util.concurrent并发工具包

    如何启动:以win7系统为例,最好jdk8 1.打开cmd,cd到jdk的path,本机是:cd C:\Java\jdk6\bin ...java -cp D:\javaConcurrentAnimated.jar vgrazi.concurrent.samples.launcher.ConcurrentExampleLauncher

    asm-util-3.2.jar

    asm-util-3.2.jarasm-util-3.2.jarasm-util-3.2.jarasm-util-3.2.jarasm-util-3.2.jarasm-util-3.2.jarasm-util-3.2.jarasm-util-3.2.jarasm-util-3.2.jarasm-util-3.2.jarasm-util-3.2.jarasm-util-3.2.jarasm-util...

    backport-util-concurrent-java12-3.1-sources.jar

    官方版本,亲测可用

    android.util.Base64类

    android.util.Base64类

Global site tag (gtag.js) - Google Analytics