<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ゞ沉默是金ゞ

    魚離不開水,但是沒有說不離開哪滴水.
    posts - 98,comments - 104,trackbacks - 0
    In this article we will look into Atomic Variables which can help us to write lock free and wait free algorithms which were not possible prior to Java 5.0.

    Two main points about Atomic Variables are 

       1. Help to write lock free and wait free algorithm

    Under high contention ( lots of thread are fighting for lock ), JVM spends more time with scheduling of threads, managing contention, queues of waiting threads and less time in doing the real work.
    This dramatically reduces the throughput of the process.
    Problem with locking:
      1) Thread in block state cannot do anything else.
        2) If the blocked thread is high priority, then its a big disaster.
        3) Can cause Dead lock
        4) Managing a Block thread is a heavy weight process, so throughput decreases.

    Soon we will see how can we write lock free algorithms using atomic variables


    2. Implement very light weight process like CAS –


    CAS (compares and swap):
           
    Let’s take an example to understand the concept of CAS. Suppose we have once variable “i” and we are doing some calculation over “I” and storing the result back into “i”. In a nutshell-
            i = someComplicateComputation( i )
    for “i” = 1,
            someComplicatedComputation(i) è 1234

    In CAS Process following happens-
            A memory location V will be defined.
            A local variable A will be defined.
            A local variable B will be defined.

    V will hold the initial value of “i”. So
            V = i =1
    A = V = 1
    B = result of that computation = 1234
    compare ( V , A )
    if
    both values are same --> replace V with B's value.
    else
            this means in the mean while someone has changed the value of V, so repeat the whole process again. Lets someone changes the value of “i”, hence V to 2.
           
                 V = 2;
                 A = V = 2
                 B = result = 3246;
                  compare ( V , A )
                            and so on...!!
           
    This is very light weight process. This CAS technique is implemented by atomic package classes.



    Example – Lets write a simple program which first increase the number by 1, then decrease the number by 1, and then increase again by 1. So overall effect is increase the number by 1. Lets run 4 threads concurrently access the method and compare the performance of AtomicInteger Vs Integer.

    package com.jovialjava.blog.threads;

    import java.util.concurrent.atomic.*;

    public class AtomicVariableExample implements Runnable {
        AtomicInteger atomic_int_1 
    = new AtomicInteger();
        AtomicInteger atomic_int_2 
    = new AtomicInteger();
        
    int int_1;
        
    int int_2;
        
    private static int count = 0;

        
    public static void main(String[] args) {
            AtomicVariableExample pr 
    = new AtomicVariableExample();
            
    new Thread(pr).start();// 1 0 1
            new Thread(pr).start();// 2 1 2
            new Thread(pr).start(); // 3 2 3
            new Thread(pr).start(); // 4 3 4
            while (true) {
                
    if (count == 4) {
                    System.out.println(pr.atomic_int_1.get());
                    System.out.println(pr.int_1);
                    
    break;
                }
            }

        }

        
    public void run() {
            System.out.println(
    "Inside run method");
            doCalc();

        }

        
    private void doCalc() {
            
    try {
                atomic_int_2 
    = atomic_int_1;
                int_2 
    = int_1;
                atomic_int_2.incrementAndGet();
                int_2 
    = int_2 + 1;
                Thread.sleep(
    1000);
                atomic_int_2.decrementAndGet();
                int_2 
    = int_2 - 1;
                Thread.sleep(
    1000);
                atomic_int_2.incrementAndGet();
                int_2 
    = int_2 + 1;
                Thread.sleep(
    1000);
                atomic_int_1 
    = atomic_int_2;
                int_1 
    = int_2;
                
    synchronized (this) {
                    count
    ++;
                }
            } 
    catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    posted on 2012-08-06 10:50 ゞ沉默是金ゞ 閱讀(952) 評論(0)  編輯  收藏 所屬分類: Java SE
    主站蜘蛛池模板: 中文字幕亚洲综合精品一区| 中文字幕专区在线亚洲| 亚洲噜噜噜噜噜影院在线播放| 国产在线观看免费av站| 不卡一卡二卡三亚洲| 精品国产污污免费网站入口在线| 亚洲天堂中文字幕在线| 久久久精品视频免费观看 | 久久亚洲AV成人无码| 美女内射无套日韩免费播放| 久久精品国产亚洲AV大全| 99国产精品永久免费视频| 亚洲xxxx18| 日韩一级免费视频| 无码日韩人妻AV一区免费l | 日韩激情淫片免费看| 狠狠综合亚洲综合亚洲色| 亚洲成a人一区二区三区| 亚洲第一视频在线观看免费| 亚洲av综合av一区| 国产免费女女脚奴视频网 | 久久精品国产亚洲AV忘忧草18| 最近免费中文字幕大全| 国产青草亚洲香蕉精品久久| 亚洲av无码成人精品区在线播放| 花蝴蝶免费视频在线观看高清版 | 草久免费在线观看网站| 亚洲精品你懂的在线观看| 亚洲精品视频免费看| 亚洲成aⅴ人片久青草影院按摩| 亚洲成网777777国产精品| 亚在线观看免费视频入口| 亚洲人成小说网站色| 亚洲午夜精品第一区二区8050| 蜜桃成人无码区免费视频网站| 亚洲国产综合AV在线观看| 精品久久久久久亚洲| 99视频全部免费精品全部四虎| 一级a性色生活片久久无少妇一级婬片免费放| 国产精品亚洲A∨天堂不卡 | 亚洲精品高清在线|