第一个程序

public class Test06 implements Runnable{

    public int a = 0;

    public static void main(String[] args) throws InterruptedException {
Test06 r = new Test06();
Thread[] t = new Thread[100];
for(int i = 0;i < 100;i++)
t[i] = new Thread(r);
for(int j = 0;j < 100;j++)
t[j].start();
Thread.currentThread().sleep(2000);
System.out.println(r.a); } @Override
public void run() {
int b = a+1;
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
a = b;
} }

输出为?

第二个程序

public class Test06 implements Runnable{

    public int a = 0;

    public static void main(String[] args) throws InterruptedException {
Test06 r = new Test06();
Thread[] t = new Thread[100];
for(int i = 0;i < 100;i++)
t[i] = new Thread(r);
for(int j = 0;j < 100;j++)
t[j].start();
Thread.currentThread().sleep(2000);
System.out.println(r.a); } @Override
public synchronized void run() {
int b = a+1;
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
a = b;
} }

输出为?

第三个程序

public class Test06 implements Runnable{

    public volatile int a = 0;

    public static void main(String[] args) throws InterruptedException {
Test06 r = new Test06();
Thread[] t = new Thread[100];
for(int i = 0;i < 100;i++)
t[i] = new Thread(r);
for(int j = 0;j < 100;j++)
t[j].start();
Thread.currentThread().sleep(2000);
System.out.println(r.a); } @Override
public void run() {
int b = a+1;
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
a = b;
} }

输出为?

三个程序中只有第二个能保证输出的为100

synchronized和vilatile的更多相关文章

  1. 高并发编程之synchronized

    一.什么是线程? 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成.另外,线程 ...

  2. java 多线程 Synchronized方法和方法块 synchronized(this)和synchronized(object)的理解

    synchronized 关键字,它包括两种用法:synchronized 方法和 synchronized 块. 1. synchronized 方法:通过在方法声明中加入 synchronized ...

  3. 单例模式中用volatile和synchronized来满足双重检查锁机制

    背景:我们在实现单例模式的时候往往会忽略掉多线程的情况,就是写的代码在单线程的情况下是没问题的,但是一碰到多个线程的时候,由于代码没写好,就会引发很多问题,而且这些问题都是很隐蔽和很难排查的. 例子1 ...

  4. Thread 学习记录 <1> -- volatile和synchronized

    恐怕比较一下volatile和synchronized的不同是最容易解释清楚的.volatile是变量修饰符,而synchronized则作用于一段代码或方法:看如下三句get代码: int i1;  ...

  5. synchronized使用说明

    好久没有更新博客了,今天试着用简单的语言把synchronized的使用说清楚. synchronized是什么? synchronized是用来保证在多线程环境下代码同步执行的可重入的互斥锁.所谓互 ...

  6. 【Java并发系列04】线程锁synchronized和Lock和volatile和Condition

    img { border: solid 1px } 一.前言 多线程怎么防止竞争资源,即防止对同一资源进行并发操作,那就是使用加锁机制.这是Java并发编程中必须要理解的一个知识点.其实使用起来还是比 ...

  7. (转)Lock和synchronized比较详解

    今天看了并发实践这本书的ReentantLock这章,感觉对ReentantLock还是不够熟悉,有许多疑问,所有在网上找了很多文章看了一下,总体说的不够详细,重点和焦点问题没有谈到,但这篇文章相当不 ...

  8. Synchronized同步性与可见性

    Synchronized是具有同步性与可见性的,那么什么是同步性与可见性呢? (1)同步性:同步性就是一个事物要么一起成功,要么一起失败,可谓是有福同享有难同当,就像A有10000去银行转5000给身 ...

  9. 基于synchronized 或 ReadWriteLock实现 简单缓存机制

    package cn.xxx.xxx; import java.util.HashMap; import java.util.Map; import java.util.concurrent.lock ...

随机推荐

  1. C#中var关键字【转】

    [转]http://blog.csdn.net/courageously/article/details/5695626 var关键字是C# 3.0开始新增的特性,称为推断类型 . 可以赋予局部变量推 ...

  2. xcode UIButton创建、监听按钮点击、自定义按钮 、状态 、内边距

    代码创建 //创建UIButton UIButton * btnType=[[UIButton alloc]init]; //设置UIControlStateNormal状态下的文字颜色 [btnTy ...

  3. Fix Some bytes have been replaced with the Unicode substitution character while loading file XXX.cs with Chinese Simplified (GB2312) encoding

    When we use <strong>visual studio</strong> open source file or any other file, we may en ...

  4. c_str()

    1.string类成员函数c_str()的原型: const char *c_str()const;//返回一个以null终止的c字符串 2.c_str()函数返回一个指向正规c字符串的指针,内容和s ...

  5. 在Eclipse中使用Maven构建SpringMVC项目

    环境搭建 安装JDK, Eclipse, Tomcat等 – 请参考网上常见攻略. 安装Maven: 下载需要的Maven 版本( http://maven.apache.org/download.c ...

  6. MyEclipse下Struts2配置使用和Ajax、JSON的配合

    原创文章,转载请注明:MyEclipse下Struts2配置使用和Ajax.JSON的配合  By Lucio.Yang 新手,初学Struts2的配置,同时尝试与Ajax通过JSON交互.首先介绍M ...

  7. Windows 安装 psutil

    第一步:下载pustil 网址:https://pipy.python.org  (http://yunpan.cn/cJg8aQpYwqfzh (提取码:7fa7)) 第二步:安装这个就没有什么好说 ...

  8. td之overflow:hidden 多余文本隐藏效果

    td之overflow:hidden 多余文本隐藏效果 方法1: table-layout: fixed; width: 200px; 语法: table-layout : auto | fixed ...

  9. JS严格模式和非严格模式的区别

    严格模式和非严格模式的区别 //f1.js 'use strice'; //整个js文件都是严格模式下执行的 var n = 1; var foo = function(){...}; //... v ...

  10. 网络上USB后面跟AF或AM接口的意思

    AM to micro 5pin或AF to micro 5pin 这里的AM或AF是Type a male和Type a female的简写 Type a male:A型男士 Type a fema ...