一个解释volatile关键字作用最好的例子

volatile
使用场景:1、防止指令重排序,比如double check
2、状态量标记: private volatile boolean canRun = true;

public class VolatileTest {
//如果把volatile去掉,进不了READER线程;
//不去掉的话UPDATER线程执行完毕执行READER线程 ,
//循环输出Update the value to。。。The value updated to 。。
    private static volatile int INIT_VALUE = 0;

    private final static int MAX_LIMIT = 500;

    public static void main(String[] args) {
        new Thread(() -> {
            int localValue = INIT_VALUE;
            while (localValue < MAX_LIMIT) {
                if (localValue != INIT_VALUE) {
                    System.out.printf("The value updated to [%d]\n", INIT_VALUE);
                    localValue = INIT_VALUE;
                }
            }
        }, "READER").start();

        new Thread(() -> {
            int localValue = INIT_VALUE;
            while (INIT_VALUE < MAX_LIMIT) {
                System.out.printf("Update the value to [%d]\n", ++localValue);
                INIT_VALUE = localValue;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "UPDATER").start();
    }

输出结果

Update the value to [1]
The value updated to [1]
Update the value to [2]
The value updated to [2]
Update the value to [3]
The value updated to [3]
Update the value to [4]
The value updated to [4]
Update the value to [5]
The value updated to [5]
Update the value to [6]
The value updated to [6]
Update the value to [7]
The value updated to [7]
Update the value to [8]
The value updated to [8]
Update the value to [9]
The value updated to [9]
Update the value to [10]
The value updated to [10]
Update the value to [11]
The value updated to [11]
Update the value to [12]
The value updated to [12]
Update the value to [13]
The value updated to [13]
Update the value to [14]
The value updated to [14]
Update the value to [15]
The value updated to [15]
Update the value to [16]
The value updated to [16]
Update the value to [17]
The value updated to [17]
Update the value to [18]
The value updated to [18]
Update the value to [19]
The value updated to [19]
Update the value to [20]
The value updated to [20]
Update the value to [21]
The value updated to [21]
Update the value to [22]
The value updated to [22]
Update the value to [23]
The value updated to [23]
Update the value to [24]
The value updated to [24]
Update the value to [25]
The value updated to [25]
Update the value to [26]
The value updated to [26]
Update the value to [27]
The value updated to [27]
Update the value to [28]
The value updated to [28]
Update the value to [29]
The value updated to [29]
Update the value to [30]
The value updated to [30]
Update the value to [31]
The value updated to [31]
Update the value to [32]
The value updated to [32]
Update the value to [33]
The value updated to [33]
Update the value to [34]
The value updated to [34]
Update the value to [35]
The value updated to [35]
Update the value to [36]
The value updated to [36]
Update the value to [37]
The value updated to [37]
Update the value to [38]
The value updated to [38]
Update the value to [39]
The value updated to [39]
Update the value to [40]
The value updated to [40]
Update the value to [41]
The value updated to [41]
Update the value to [42]
The value updated to [42]
Update the value to [43]
The value updated to [43]
Update the value to [44]
The value updated to [44]
Update the value to [45]
The value updated to [45]
Update the value to [46]
The value updated to [46]
Update the value to [47]
The value updated to [47]
Update the value to [48]
The value updated to [48]
Update the value to [49]
The value updated to [49]
Update the value to [50]
The value updated to [50]
Update the value to [51]
The value updated to [51]
Update the value to [52]
The value updated to [52]
Update the value to [53]
The value updated to [53]
Update the value to [54]
The value updated to [54]
Update the value to [55]
The value updated to [55]
Update the value to [56]
The value updated to [56]
Update the value to [57]
The value updated to [57]
Update the value to [58]
The value updated to [58]
Update the value to [59]
The value updated to [59]
Update the value to [60]
The value updated to [60]
Update the value to [61]
The value updated to [61]
Update the value to [62]
The value updated to [62]
Update the value to [63]
The value updated to [63]
Update the value to [64]
The value updated to [64]
Update the value to [65]
The value updated to [65]
Update the value to [66]
The value updated to [66]
Update the value to [67]
The value updated to [67]
Update the value to [68]
The value updated to [68]
Update the value to [69]
The value updated to [69]
Update the value to [70]
The value updated to [70]
Update the value to [71]
The value updated to [71]
Update the value to [72]
The value updated to [72]
Update the value to [73]
The value updated to [73]
Update the value to [74]
The value updated to [74]
Update the value to [75]
The value updated to [75]
Update the value to [76]
The value updated to [76]
Update the value to [77]
The value updated to [77]
Update the value to [78]
The value updated to [78]
Update the value to [79]
The value updated to [79]
Update the value to [80]
The value updated to [80]
Update the value to [81]
The value updated to [81]
Update the value to [82]
The value updated to [82]
Update the value to [83]
The value updated to [83]
Update the value to [84]
The value updated to [84]
Update the value to [85]
The value updated to [85]
Update the value to [86]
The value updated to [86]
Update the value to [87]
The value updated to [87]
Update the value to [88]
The value updated to [88]
Update the value to [89]
The value updated to [89]
Update the value to [90]
The value updated to [90]
Update the value to [91]
The value updated to [91]
Update the value to [92]
The value updated to [92]
Update the value to [93]
The value updated to [93]
Update the value to [94]
The value updated to [94]
Update the value to [95]
The value updated to [95]
Update the value to [96]
The value updated to [96]
Update the value to [97]
The value updated to [97]
Update the value to [98]
The value updated to [98]
Update the value to [99]
The value updated to [99]
Update the value to [100]
The value updated to [100]
Update the value to [101]
The value updated to [101]
Update the value to [102]
The value updated to [102]
Update the value to [103]
The value updated to [103]
Update the value to [104]
The value updated to [104]
Update the value to [105]
The value updated to [105]
Update the value to [106]
The value updated to [106]
Update the value to [107]
The value updated to [107]
Update the value to [108]
The value updated to [108]
Update the value to [109]
The value updated to [109]
Update the value to [110]
The value updated to [110]
Update the value to [111]
The value updated to [111]
Update the value to [112]
The value updated to [112]
Update the value to [113]
The value updated to [113]
Update the value to [114]
The value updated to [114]
Update the value to [115]
The value updated to [115]
Update the value to [116]
The value updated to [116]
Update the value to [117]
The value updated to [117]
Update the value to [118]
The value updated to [118]
Update the value to [119]
The value updated to [119]
Update the value to [120]
The value updated to [120]
Update the value to [121]
The value updated to [121]
Update the value to [122]
The value updated to [122]
Update the value to [123]
The value updated to [123]
Update the value to [124]
The value updated to [124]
Update the value to [125]
The value updated to [125]
Update the value to [126]
The value updated to [126]
Update the value to [127]
The value updated to [127]
Update the value to [128]
The value updated to [128]
Update the value to [129]
The value updated to [129]
Update the value to [130]
The value updated to [130]
Update the value to [131]
The value updated to [131]
Update the value to [132]
The value updated to [132]
Update the value to [133]
The value updated to [133]
Update the value to [134]
The value updated to [134]
Update the value to [135]
The value updated to [135]
Update the value to [136]
The value updated to [136]
Update the value to [137]
The value updated to [137]
Update the value to [138]
The value updated to [138]
Update the value to [139]
The value updated to [139]
Update the value to [140]
The value updated to [140]
Update the value to [141]
The value updated to [141]
Update the value to [142]
The value updated to [142]
Update the value to [143]
The value updated to [143]
Update the value to [144]
The value updated to [144]
Update the value to [145]
The value updated to [145]
Update the value to [146]
The value updated to [146]
Update the value to [147]
The value updated to [147]
Update the value to [148]
The value updated to [148]
Update the value to [149]
The value updated to [149]
Update the value to [150]
The value updated to [150]
Update the value to [151]
The value updated to [151]
Update the value to [152]
The value updated to [152]
Update the value to [153]
The value updated to [153]
Update the value to [154]
The value updated to [154]
Update the value to [155]
The value updated to [155]
Update the value to [156]
The value updated to [156]
Update the value to [157]
The value updated to [157]
Update the value to [158]
The value updated to [158]
Update the value to [159]
The value updated to [159]
Update the value to [160]
The value updated to [160]
Update the value to [161]
The value updated to [161]
Update the value to [162]
The value updated to [162]
Update the value to [163]
The value updated to [163]
Update the value to [164]
The value updated to [164]
Update the value to [165]
The value updated to [165]
Update the value to [166]
The value updated to [166]
Update the value to [167]
The value updated to [167]
Update the value to [168]
The value updated to [168]
Update the value to [169]
The value updated to [169]
Update the value to [170]
The value updated to [170]
Update the value to [171]
The value updated to [171]
Update the value to [172]
The value updated to [172]
Update the value to [173]
The value updated to [173]
Update the value to [174]
The value updated to [174]
Update the value to [175]
The value updated to [175]
Update the value to [176]
The value updated to [176]
Update the value to [177]
The value updated to [177]
Update the value to [178]
The value updated to [178]
Update the value to [179]
The value updated to [179]
Update the value to [180]
The value updated to [180]
Update the value to [181]
The value updated to [181]
Update the value to [182]
The value updated to [182]
Update the value to [183]
The value updated to [183]
Update the value to [184]
The value updated to [184]
Update the value to [185]
The value updated to [185]
Update the value to [186]
The value updated to [186]
Update the value to [187]
The value updated to [187]
Update the value to [188]
The value updated to [188]
Update the value to [189]
The value updated to [189]
Update the value to [190]
The value updated to [190]
Update the value to [191]
The value updated to [191]
Update the value to [192]
The value updated to [192]
Update the value to [193]
The value updated to [193]
Update the value to [194]
The value updated to [194]
Update the value to [195]
The value updated to [195]
Update the value to [196]
The value updated to [196]
Update the value to [197]
The value updated to [197]
Update the value to [198]
The value updated to [198]
Update the value to [199]
The value updated to [199]
Update the value to [200]
The value updated to [200]
Update the value to [201]
The value updated to [201]
Update the value to [202]
The value updated to [202]
Update the value to [203]
The value updated to [203]
Update the value to [204]
The value updated to [204]
Update the value to [205]
The value updated to [205]
Update the value to [206]
The value updated to [206]
Update the value to [207]
The value updated to [207]
Update the value to [208]
The value updated to [208]
Update the value to [209]
The value updated to [209]
Update the value to [210]
The value updated to [210]
Update the value to [211]
The value updated to [211]
Update the value to [212]
The value updated to [212]
Update the value to [213]
The value updated to [213]
Update the value to [214]
The value updated to [214]
Update the value to [215]
The value updated to [215]
Update the value to [216]
The value updated to [216]
Update the value to [217]
The value updated to [217]
Update the value to [218]
The value updated to [218]
Update the value to [219]
The value updated to [219]
Update the value to [220]
The value updated to [220]
Update the value to [221]
The value updated to [221]
Update the value to [222]
The value updated to [222]
Update the value to [223]
The value updated to [223]
Update the value to [224]
The value updated to [224]
Update the value to [225]
The value updated to [225]
Update the value to [226]
The value updated to [226]
Update the value to [227]
The value updated to [227]
Update the value to [228]
The value updated to [228]
Update the value to [229]
The value updated to [229]
Update the value to [230]
The value updated to [230]
Update the value to [231]
The value updated to [231]
Update the value to [232]
The value updated to [232]
Update the value to [233]
The value updated to [233]
Update the value to [234]
The value updated to [234]
Update the value to [235]
The value updated to [235]
Update the value to [236]
The value updated to [236]
Update the value to [237]
The value updated to [237]
Update the value to [238]
The value updated to [238]
Update the value to [239]
The value updated to [239]
Update the value to [240]
The value updated to [240]
Update the value to [241]
The value updated to [241]
Update the value to [242]
The value updated to [242]
Update the value to [243]
The value updated to [243]
Update the value to [244]
The value updated to [244]
Update the value to [245]
The value updated to [245]
Update the value to [246]
The value updated to [246]
Update the value to [247]
The value updated to [247]
Update the value to [248]
The value updated to [248]
Update the value to [249]
The value updated to [249]
Update the value to [250]
The value updated to [250]
Update the value to [251]
The value updated to [251]
Update the value to [252]
The value updated to [252]
Update the value to [253]
The value updated to [253]
Update the value to [254]
The value updated to [254]
Update the value to [255]
The value updated to [255]
Update the value to [256]
The value updated to [256]
Update the value to [257]
The value updated to [257]
Update the value to [258]
The value updated to [258]
Update the value to [259]
The value updated to [259]
Update the value to [260]
The value updated to [260]
Update the value to [261]
The value updated to [261]
Update the value to [262]
The value updated to [262]
Update the value to [263]
The value updated to [263]
Update the value to [264]
The value updated to [264]
Update the value to [265]
The value updated to [265]
Update the value to [266]
The value updated to [266]
Update the value to [267]
The value updated to [267]
Update the value to [268]
The value updated to [268]
Update the value to [269]
The value updated to [269]
Update the value to [270]
The value updated to [270]
Update the value to [271]
The value updated to [271]
Update the value to [272]
The value updated to [272]
Update the value to [273]
The value updated to [273]
Update the value to [274]
The value updated to [274]
Update the value to [275]
The value updated to [275]
Update the value to [276]
The value updated to [276]
Update the value to [277]
The value updated to [277]
Update the value to [278]
The value updated to [278]
Update the value to [279]
The value updated to [279]
Update the value to [280]
The value updated to [280]
Update the value to [281]
The value updated to [281]
Update the value to [282]
The value updated to [282]
Update the value to [283]
The value updated to [283]
Update the value to [284]
The value updated to [284]
Update the value to [285]
The value updated to [285]
Update the value to [286]
The value updated to [286]
Update the value to [287]
The value updated to [287]
Update the value to [288]
The value updated to [288]
Update the value to [289]
The value updated to [289]
Update the value to [290]
The value updated to [290]
Update the value to [291]
The value updated to [291]
Update the value to [292]
The value updated to [292]
Update the value to [293]
The value updated to [293]
Update the value to [294]
The value updated to [294]
Update the value to [295]
The value updated to [295]
Update the value to [296]
The value updated to [296]
Update the value to [297]
The value updated to [297]
Update the value to [298]
The value updated to [298]
Update the value to [299]
The value updated to [299]
Update the value to [300]
The value updated to [300]
Update the value to [301]
The value updated to [301]
Update the value to [302]
The value updated to [302]
Update the value to [303]
The value updated to [303]
Update the value to [304]
The value updated to [304]
Update the value to [305]
The value updated to [305]
Update the value to [306]
The value updated to [306]
Update the value to [307]
The value updated to [307]
Update the value to [308]
The value updated to [308]
Update the value to [309]
The value updated to [309]
Update the value to [310]
The value updated to [310]
Update the value to [311]
The value updated to [311]
Update the value to [312]
The value updated to [312]
Update the value to [313]
The value updated to [313]
Update the value to [314]
The value updated to [314]
Update the value to [315]
The value updated to [315]
Update the value to [316]
The value updated to [316]
Update the value to [317]
The value updated to [317]
Update the value to [318]
The value updated to [318]
Update the value to [319]
The value updated to [319]
Update the value to [320]
The value updated to [320]
Update the value to [321]
The value updated to [321]
Update the value to [322]
The value updated to [322]
Update the value to [323]
The value updated to [323]
Update the value to [324]
The value updated to [324]
Update the value to [325]
The value updated to [325]
Update the value to [326]
The value updated to [326]
Update the value to [327]
The value updated to [327]
Update the value to [328]
The value updated to [328]
Update the value to [329]
The value updated to [329]
Update the value to [330]
The value updated to [330]
Update the value to [331]
The value updated to [331]
Update the value to [332]
The value updated to [332]
Update the value to [333]
The value updated to [333]
Update the value to [334]
The value updated to [334]
Update the value to [335]
The value updated to [335]
Update the value to [336]
The value updated to [336]
Update the value to [337]
The value updated to [337]
Update the value to [338]
The value updated to [338]
Update the value to [339]
The value updated to [339]
Update the value to [340]
The value updated to [340]
Update the value to [341]
The value updated to [341]
Update the value to [342]
The value updated to [342]
Update the value to [343]
The value updated to [343]
Update the value to [344]
The value updated to [344]
Update the value to [345]
The value updated to [345]
Update the value to [346]
The value updated to [346]
Update the value to [347]
The value updated to [347]
Update the value to [348]
The value updated to [348]
Update the value to [349]
The value updated to [349]
Update the value to [350]
The value updated to [350]
Update the value to [351]
The value updated to [351]
Update the value to [352]
The value updated to [352]
Update the value to [353]
The value updated to [353]
Update the value to [354]
The value updated to [354]
Update the value to [355]
The value updated to [355]
Update the value to [356]
The value updated to [356]
Update the value to [357]
The value updated to [357]
Update the value to [358]
The value updated to [358]
Update the value to [359]
The value updated to [359]
Update the value to [360]
The value updated to [360]
Update the value to [361]
The value updated to [361]
Update the value to [362]
The value updated to [362]
Update the value to [363]
The value updated to [363]
Update the value to [364]
The value updated to [364]
Update the value to [365]
The value updated to [365]
Update the value to [366]
The value updated to [366]
Update the value to [367]
The value updated to [367]
Update the value to [368]
The value updated to [368]
Update the value to [369]
The value updated to [369]
Update the value to [370]
The value updated to [370]
Update the value to [371]
The value updated to [371]
Update the value to [372]
The value updated to [372]
Update the value to [373]
The value updated to [373]
Update the value to [374]
The value updated to [374]
Update the value to [375]
The value updated to [375]
Update the value to [376]
The value updated to [376]
Update the value to [377]
The value updated to [377]
Update the value to [378]
The value updated to [378]
Update the value to [379]
The value updated to [379]
Update the value to [380]
The value updated to [380]
Update the value to [381]
The value updated to [381]
Update the value to [382]
The value updated to [382]
Update the value to [383]
The value updated to [383]
Update the value to [384]
The value updated to [384]
Update the value to [385]
The value updated to [385]
Update the value to [386]
The value updated to [386]
Update the value to [387]
The value updated to [387]
Update the value to [388]
The value updated to [388]
Update the value to [389]
The value updated to [389]
Update the value to [390]
The value updated to [390]
Update the value to [391]
The value updated to [391]
Update the value to [392]
The value updated to [392]
Update the value to [393]
The value updated to [393]
Update the value to [394]
The value updated to [394]
Update the value to [395]
The value updated to [395]
Update the value to [396]
The value updated to [396]
Update the value to [397]
The value updated to [397]
Update the value to [398]
The value updated to [398]
Update the value to [399]
The value updated to [399]
Update the value to [400]
The value updated to [400]
Update the value to [401]
The value updated to [401]
Update the value to [402]
The value updated to [402]
Update the value to [403]
The value updated to [403]
Update the value to [404]
The value updated to [404]
Update the value to [405]
The value updated to [405]
Update the value to [406]
The value updated to [406]
Update the value to [407]
The value updated to [407]
Update the value to [408]
The value updated to [408]
Update the value to [409]
The value updated to [409]
Update the value to [410]
The value updated to [410]
Update the value to [411]
The value updated to [411]
Update the value to [412]
The value updated to [412]
Update the value to [413]
The value updated to [413]
Update the value to [414]
The value updated to [414]
Update the value to [415]
The value updated to [415]
Update the value to [416]
The value updated to [416]
Update the value to [417]
The value updated to [417]
Update the value to [418]
The value updated to [418]
Update the value to [419]
The value updated to [419]
Update the value to [420]
The value updated to [420]
Update the value to [421]
The value updated to [421]
Update the value to [422]
The value updated to [422]
Update the value to [423]
The value updated to [423]
Update the value to [424]
The value updated to [424]
Update the value to [425]
The value updated to [425]
Update the value to [426]
The value updated to [426]
Update the value to [427]
The value updated to [427]
Update the value to [428]
The value updated to [428]
Update the value to [429]
The value updated to [429]
Update the value to [430]
The value updated to [430]
Update the value to [431]
The value updated to [431]
Update the value to [432]
The value updated to [432]
Update the value to [433]
The value updated to [433]
Update the value to [434]
The value updated to [434]
Update the value to [435]
The value updated to [435]
Update the value to [436]
The value updated to [436]
Update the value to [437]
The value updated to [437]
Update the value to [438]
The value updated to [438]
Update the value to [439]
The value updated to [439]
Update the value to [440]
The value updated to [440]
Update the value to [441]
The value updated to [441]
Update the value to [442]
The value updated to [442]
Update the value to [443]
The value updated to [443]
Update the value to [444]
The value updated to [444]
Update the value to [445]
The value updated to [445]
Update the value to [446]
The value updated to [446]
Update the value to [447]
The value updated to [447]
Update the value to [448]
The value updated to [448]
Update the value to [449]
The value updated to [449]
Update the value to [450]
The value updated to [450]
Update the value to [451]
The value updated to [451]
Update the value to [452]
The value updated to [452]
Update the value to [453]
The value updated to [453]
Update the value to [454]
The value updated to [454]
Update the value to [455]
The value updated to [455]
Update the value to [456]
The value updated to [456]
Update the value to [457]
The value updated to [457]
Update the value to [458]
The value updated to [458]
Update the value to [459]
The value updated to [459]
Update the value to [460]
The value updated to [460]
Update the value to [461]
The value updated to [461]
Update the value to [462]
The value updated to [462]
Update the value to [463]
The value updated to [463]
Update the value to [464]
The value updated to [464]
Update the value to [465]
The value updated to [465]
Update the value to [466]
The value updated to [466]
Update the value to [467]
The value updated to [467]
Update the value to [468]
The value updated to [468]
Update the value to [469]
The value updated to [469]
Update the value to [470]
The value updated to [470]
Update the value to [471]
The value updated to [471]
Update the value to [472]
The value updated to [472]
Update the value to [473]
The value updated to [473]
Update the value to [474]
The value updated to [474]
Update the value to [475]
The value updated to [475]
Update the value to [476]
The value updated to [476]
Update the value to [477]
The value updated to [477]
Update the value to [478]
The value updated to [478]
Update the value to [479]
The value updated to [479]
Update the value to [480]
The value updated to [480]
Update the value to [481]
The value updated to [481]
Update the value to [482]
The value updated to [482]
Update the value to [483]
The value updated to [483]
Update the value to [484]
The value updated to [484]
Update the value to [485]
The value updated to [485]
Update the value to [486]
The value updated to [486]
Update the value to [487]
The value updated to [487]
Update the value to [488]
The value updated to [488]
Update the value to [489]
The value updated to [489]
Update the value to [490]
The value updated to [490]
Update the value to [491]
The value updated to [491]
Update the value to [492]
The value updated to [492]
Update the value to [493]
The value updated to [493]
Update the value to [494]
The value updated to [494]
Update the value to [495]
The value updated to [495]
Update the value to [496]
The value updated to [496]
Update the value to [497]
The value updated to [497]
Update the value to [498]
The value updated to [498]
Update the value to [499]
The value updated to [499]
Update the value to [500]
The value updated to [500]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值