i++와++i가 다중 라인에서의 표현,volatile와 잠금

11607 단어 os
자물쇠를 채워야만 쟁취한 결과를 얻을 수 있다.
콘솔 출력:
i++ 결과: 1000012113++i 결과: 1000003505 volatile i++ 결과: 1018584689 volatile++i 결과: 995483979 자물쇠 i++ 결과: 2000000000 자물쇠 +i 결과: 200000000000
테스트 코드는 다음과 같습니다.
/
public class ThreadTest1 {
    private static long i = 0;
    private static volatile long m = 0;

    public static void main(String[] args) throws Exception{
        System.out.println("i++   :");
        new ThreadTest1().test();
        ThreadTest1.i = 0;//  

        System.out.println("++i   :");
        new ThreadTest1().test1();
        ThreadTest1.i = 0;//  

        System.out.println("volatile i++   :");
        new ThreadTest1().test2();
        ThreadTest1.m = 0;//  

        System.out.println("volatile ++i   :");
        new ThreadTest1().test3();
        ThreadTest1.m = 0;//  

        System.out.println("   i++   :");
        new ThreadTest1().test4();
        ThreadTest1.i = 0;//  

        System.out.println("    ++i   :");
        new ThreadTest1().test5();
        ThreadTest1.i = 0;//  


    }

    private void test() throws Exception{
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 1000000000; i++){
                    ThreadTest1.i++;
                }
            }
        };
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(ThreadTest1.i);
    }

    private void test1() throws Exception{
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 1000000000; i++){
                    ++ThreadTest1.i;
                }
            }
        };
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(ThreadTest1.i);
    }

    private void test2() throws Exception{
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 1000000000; i++){
                    ThreadTest1.m++;
                }
            }
        };
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(ThreadTest1.m);
    }

    private void test3() throws Exception{
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 1000000000; i++){
                    ++ThreadTest1.m;
                }
            }
        };
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(ThreadTest1.m);
    }

    private void test4() throws Exception{
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                synchronized (ThreadTest1.class){
                    for (int i = 0; i < 1000000000; i++){
                        ThreadTest1.i++;
                    }
                }
            }
        };
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(ThreadTest1.i);
    }

    private void test5() throws Exception{
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                synchronized (ThreadTest1.class){
                    for (int i = 0; i < 1000000000; i++){
                        ++ThreadTest1.i;
                    }
                }
            }
        };
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(ThreadTest1.i);
    }
}

좋은 웹페이지 즐겨찾기