java volatile应用

多线中使用到while判断,被判断的boolean值在多线程中会出现获取不到最新值,导致while无法结束,此时可以使用volatile标注此boolean变量。

package study.book.springinaction.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class JZhouJdbcUtils {
    private static volatile boolean b1 = false;//此处如果不标注volatile将会导致下面的runnable5里的while循环无法结束
    private static boolean b2 = false;
    private static boolean b3 = false;
    private static boolean b4 = false;
    private static List lst = new ArrayList<>();
    private static List lst2 = new ArrayList<>();
    private static List lst3 = new ArrayList<>();
    private static List lst4 = new ArrayList<>();
    private static List lst5 = new ArrayList<>();
    public static void main(String[] args){
        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(2);
                    b4=true;
                    System.out.println(Thread.currentThread().getName()+" sleep 2 second");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(3);
                    b3=true;
                    System.out.println(Thread.currentThread().getName()+" sleep 3 second");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Runnable runnable3 = new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(4);
                    b2=true;
                    System.out.println(Thread.currentThread().getName()+" sleep 4 second");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Runnable runnable4 = new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(5);
                    b1=true;
                    System.out.println(Thread.currentThread().getName()+" sleep 5 second");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Runnable runnable5 = new Runnable() {
            @Override
            public void run() {
                while (!b1){
                    if(b2){
                        if(lst2.size()==0){
                            System.out.println("=====2===");
                        }
                        lst2.add("");
                    }
                    if(b3){
                        if(lst3.size()==0){
                            System.out.println("=====3===");
                        }

                        lst3.add("");
                    }
                    if(b4){
                        if(lst4.size()==0){
                            System.out.println("=====4===");
                        }
                        lst4.add("");
                    }
                    //synchronized (this){
                    //包裹上面的代码不使用volatile可以达到相同的效果,但是性能上会差一些
                    //}

                }
                System.out.println("lst.size()="+lst.size());
            }
        };

        ExecutorService exec = Executors.newCachedThreadPool();
        exec.execute(runnable1);
        exec.execute(runnable2);
        exec.execute(runnable3);
        exec.execute(runnable4);
        exec.execute(runnable5);
        exec.shutdown();
    }


}

推荐博文

Java多线程编程的常见陷阱
java中volatile关键字的含义
正确使用 Volatile 变量
java 垃圾回收总结(1)
java垃圾回收总结(2)

你可能感兴趣的:(多线程)