线程安全——Atomic系列类(主要实现原子类)

Atomic系列类封装了一系列的基础类型和对象操作,其主要目的就是为了实现原子性,主要核心类如下:

AtomicInteger

AtomicLong

AtomicBoolean

AtomicIntegerArray

AtomicLongArray

AtomicReference

原子性,多线程中能并发操作,保证数据准确

AtomicInteger与synchronized(将add操作变成一个原子操作)能保证原子性操作,(最终结果1000)能保证方法内运行中不会出现617,613之类的数据

package com.bfxy.thread.core.cas;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class UseAtomic {

	private static /* int count = 0; */ AtomicInteger  count = new AtomicInteger(0);
	
	public synchronized int add(){
		//return count.addAndGet(10);
		count.addAndGet(3);  // + 3
		count.addAndGet(4);  // + 4
		count.addAndGet(2);  // + 2 
		count.addAndGet(1);  // + 1
		
		
		// 写了一段代码逻辑........ count.add(1);
		
		// 又写了一个复杂的逻辑...
		// count.add(1);
		// = 2
		
		return count.get();	
	}
	
	public static void main(String[] args) {
		UseAtomic ua = new UseAtomic();
		
		List<Thread> list = new ArrayList<>();
		
		//如果使用atomicIntger 最终的结果 一定是: 1000
		for(int i =0; i < 100; i++) {
			list.add(new Thread(new Runnable() {
				@Override
				public void run() {
					System.err.println("累计结果:" + ua.add());
				}
			}));
		}
		
		for(Thread t : list) {
			t.start();
		}
		
		
	}
	
	
}

AtomicReference引用对象

package com.bfxy.thread.core.cas;

public class Person {
	private String name;
	private int age;
	
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
    public String toString() {
        return "[name: " + this.name + ", age: " + this.age + "]";
    }
}

测试:

package com.bfxy.thread.core.cas;

public class UseAtomicReference1 {

	private static Person person;
	
	public static void main(String[] args) throws InterruptedException {
	    person = new Person("Tom", 18);

	    System.out.println("Person is " + person.toString());

	    Thread t1 = new Thread(new Task1());
	    Thread t2 = new Thread(new Task2());

	    t1.start();
	    t2.start();
	    t1.join();
	    t2.join();
	    
	    Thread.sleep(100);
	    System.out.println("Now Person is " + person.toString());
	}

	static class Task1 implements Runnable {
	    public void run() {
	        person.setAge(19);
	        person.setName("Tom1");

	        System.out.println("Thread1 Values "
	                + person.toString());
	    }
	}

	static class Task2 implements Runnable {
	    public void run() {
	        person.setAge(20);
	        person.setName("Tom2");

	        System.out.println("Thread2 Values "
	                + person.toString());
	    }
	}
}

测试2:

package com.bfxy.thread.core.cas;

import java.util.concurrent.atomic.AtomicReference;

public class UseAtomicReference2 {
	// 普通引用
	private static Person person;
	// 原子性引用
	private static AtomicReference<Person> aRperson;

	public static void main(String[] args) throws InterruptedException {
	    person = new Person("Tom", 18);
	    aRperson = new AtomicReference<Person>(person);

	    System.out.println("Atomic Person is " + aRperson.get().toString());

	    Thread t1 = new Thread(new Task1());
	    Thread t2 = new Thread(new Task2());

	    t1.start();
	    t2.start();
	    t1.join();
	    t2.join();
	    
	    Thread.sleep(500);
	    System.out.println("Now Atomic Person is " + aRperson.get().toString());
	}

	static class Task1 implements Runnable {  
	    public void run() {
	    	System.err.println("ret = " + 
	    				// C A S 原子操作
	    				aRperson.compareAndSet(  //10ms 
	    					aRperson.get(), 
	    					new Person("Tom", aRperson.get().getAge() + 1)
	    			));
	        System.out.println("Thread1 Atomic References "
	                + aRperson.get().toString());
	    }
	}

	static class Task2 implements Runnable {
	    public void run() {
	    	System.err.println("ret = " + 
	    			aRperson.compareAndSet(		//8ms
	    					aRperson.get(), 
	    					new Person("Tom", aRperson.get().getAge() + 2)
	    			));
	        System.out.println("Thread2 Atomic References "
	                + aRperson.get().toString());
	    }
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

择业

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值