博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
你真的掌握了并发编程volatile synchronized么?
阅读量:1981 次
发布时间:2019-04-27

本文共 2313 字,大约阅读时间需要 7 分钟。

先看代码:

import java.util.concurrent.atomic.AtomicInteger;/** * * @author xialuomantian */public class NewTest {    static volatile int a = 1;    static volatile int b = 1;    //static  int a = 1;    //static  int b = 1;    public static AtomicInteger aa = new AtomicInteger(0);    public static AtomicInteger bb = new AtomicInteger(0);    public static AtomicInteger ab = new AtomicInteger(0);        static void add() {        a++;        b++;    }    static synchronized void print() {        if (a > b) {            aa.getAndIncrement();                   } else if (a < b) {            bb.getAndIncrement();        } if (a == b) {            ab.getAndIncrement();        }    }    public static void main(String[] args) {        System.out.println(Thread.currentThread().getName());        for (int i = 0; i < 100000; i++) {            new Thread(() -> {                               add();                print();            }).start();        }        System.out.println("a>b:" + aa);        System.out.println("b>a:" + bb);        System.out.println("a==b:" + ab);    }}

再去掉volatile

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package com.mycompany.mytest;import java.util.concurrent.atomic.AtomicInteger;/** * * @author xialuomantian */public class NewTest {    static  int a = 1;    static  int b = 1;    public static AtomicInteger aa = new AtomicInteger(0);    public static AtomicInteger bb = new AtomicInteger(0);    public static AtomicInteger ab = new AtomicInteger(0);        static void add() {        a++;        b++;    }    static synchronized void print() {        if (a > b) {            aa.getAndIncrement();                   } else if (a < b) {            bb.getAndIncrement();        } if (a == b) {            ab.getAndIncrement();        }    }    public static void main(String[] args) {        System.out.println(Thread.currentThread().getName());        for (int i = 0; i < 100000; i++) {            new Thread(() -> {                               add();                print();            }).start();        }        System.out.println("a>b:" + aa);        System.out.println("b>a:" + bb);        System.out.println("a==b:" + ab);    }}

下面是几种结果:

转载地址:http://plrpf.baihongyu.com/

你可能感兴趣的文章
JavaScript DOM对象操作详解
查看>>
JavaScript 表单操作与MD5加密
查看>>
JAVA学习笔记4 - 循环与分支结构
查看>>
JAVA学习笔记5 - Number类,Math类,Character类,String类,StringBuffer类
查看>>
JAVA学习笔记6 - 数组
查看>>
JAVA学习笔记8 - Stream 和 File I/O
查看>>
JAVA学习笔记9 - 异常
查看>>
JAVA学习笔记10 - 继承
查看>>
JAVA学习笔记11 - 接口interface
查看>>
JAVA学习笔记12 - 包package
查看>>
Android 开发学习笔记 00 - Getting Started
查看>>
【学习笔记】Android Activity
查看>>
【学习笔记】Android Fragments
查看>>
Android使用Retrofit_00_Getting Started
查看>>
Android使用Retrofit_01_OAuth2 + GitHub
查看>>
Django oauth toolkit + Android + Retrofit 实现 OAuth2 的 access token 获取
查看>>
Android + Django + OAuth2 + Stub Authenticator
查看>>
Django + REST学习笔记
查看>>
Android Sync Adapter (使用Stub Content Provider) 笔记
查看>>
诡异的 Scroll view may have only one direct child placed within it 错误
查看>>