码迷,mamicode.com
首页 > 编程语言 > 详细

Java多线程系列一——Atomic类

时间:2017-09-17 01:26:40      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:string   oid   就会   带来   存在   定义   编程   面试官   atom   

参考资料:https://fangjian0423.github.io/2016/03/16/java-AtomicInteger-analysis/
http://www.cnblogs.com/549294286/p/3766717.html

最近面试遇到一道编程题,要求两个线程交替打印[0,100]的数字,其中一个只打印奇数,另一个只打印偶数,并且给出特别明显的提示AtomicInteger,当时我在想简直是送分题啊,但事后回想由于手写又没有记得所有API,很多地方不完美,所以面试官最后让我解释一下,回来再用IDE写一遍就顺畅多了,解题思路:

  • 定义thread0输出奇数,thread1输出偶数
  • thread0执行时,若当前值为偶数则进入waiting状态并焕醒thread1
  • thread1执行时,若当前值为奇数则进入waiting状态并焕醒thread0
  • 循环执行上两步直到当前值达到100
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: 测试AtomicInteger*/
public class AtomicIntegerTest {
    private static AtomicInteger num = new AtomicInteger(0);

    public static void main(String[] args) {
        MyThead thread0 = new MyThead(1);
        MyThead thread1 = new MyThead(0);
        thread0.start();
        thread1.start();
    }

    public static class MyThead extends Thread {

        private int v;

        public MyThead(int v) {
            this.v = v;
        }

        @Override
        public void run() {
            synchronized (num) {
                while (num.get() < 100) {
                    if (v == 1) {
                        if (num.get() % 2 == 0) {
                            try {
                                num.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            System.out.println(num.incrementAndGet());
                            num.notifyAll();
                        }
                    } else if (v == 0) {
                        if (num.get() % 2 == 1) {
                            try {
                                num.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            System.out.println(num.incrementAndGet());
                            num.notifyAll();
                        }
                    }
                }
            }
        }
    }
}

从AtomicInteger类源码可以看到,它定义了一个volatile的变量value用于存储值,而所有的操作都借助Unsafe类的操作来完成,Unsafe类的许多操作为CAS,可以理解为自带乐观锁,乐观锁就会存在ABA问题,但由于AtomicInteger保存的是int元素,这个问题几乎可以忽略,故可认为它是线程安全的。然而对于AtomicReference来说,ABA问题就可能带来致命问题,因此有了AtomicStampedReference类来解决这个问题。

Java多线程系列一——Atomic类

标签:string   oid   就会   带来   存在   定义   编程   面试官   atom   

原文地址:http://www.cnblogs.com/hiver/p/7533629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!