栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021-10-15

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-10-15

 * volatile 内存可见性   多个线程操作共享数据时,可以保存内存中的数据可见
 *             相较于synchronized是一种较为轻量级的同步策略
 * 注意:
 * 1.volatile不具备互斥性
 * 2.volatile不能保证原子性
 * jvm会给每一个线程分配独立的内存

package thread;

public class TestVolatile {
	public static void main(String[] args) {
		ThreadDemo td = new ThreadDemo();
		new Thread(td).start();
		while(true) {
//			synchronized (td) {
				if(td.isFlag()) {
					System.out.println("-------------");
					break;
//				}
			}
		}
	}
}


class ThreadDemo implements Runnable{
//	private volatile boolean flag = false;
	private  boolean flag = false;
	@Override
	public void run() {
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		flag = true;
		System.out.println("flag="+isFlag());
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	
}

* i++原子性   有读改写
 * 
 * 原子变量 java.util.concurrent.atomic 提供了常用的原子变量
 *     1. volatile 保证内存可见性
 *     2. CAS (Compare-And-Swap)算法保证数据的原子性   CAS算法是硬件对于并发操作共享数据的支持
 *         CAS包含了3个操作数
 *         内存值V
 *         预估值A
 *         更新值B
 *         当且仅当V==A时 V=B 否则什么都不做

package thread;

import java.util.concurrent.atomic.AtomicInteger;


public class TestAutomicDemo {

	public static void main(String[] args) {
		
		AutomicDemo ad = new AutomicDemo();
		for (int i = 0; i < 10; i++) {
			new Thread(ad).start();
		}
	}
}

class AutomicDemo implements Runnable{
	
	private AtomicInteger serialNum = new AtomicInteger(0);
	@Override
	public void run() {
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(getSerialNum());
	}
	public int getSerialNum() {
		return serialNum.getAndIncrement();//后++
	}
}

模拟CAS

package thread;

public class TestCompareAndSwap {

	public static void main(String[] args) {
		final CompareAndSwap cas = new CompareAndSwap();
		for (int i = 0; i < 10; i++) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					int expectevalue = cas.get();
//					System.out.println("更新前获取期望值:"+expectevalue);
					boolean ss= cas.compareAndSet(expectevalue, (int)(Math.random()*101));
					System.out.println(ss);
				}
			}).start();
		}
	}
}

class CompareAndSwap{
	private int value;
	
	public synchronized int get() {
		return this.value;
	}
	
	public synchronized int compareAndSwap(int expectevalue,int newValue) {
		int oldvalue = value;
		if(expectevalue==oldvalue) {
			this.value = newValue;
			
//			System.out.println("value被更新:"+newValue);
		}
		return oldvalue;
	}
	
	public synchronized boolean compareAndSet(int expectevalue,int newValue) {
		return expectevalue == compareAndSwap(expectevalue,newValue);
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/328424.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号