多线程-ThreadLocal,InheritableThreadLocal

ThreadLocal

变量值的共享可以使用public static变量的形式,所有的线程都使用同一个public static变量。如果想实现每一个线程都有自己的共享变量该如何解决呢?JDK中提供的ThreadLocal正是解决这样的问题。 
ThreadLocal主要解决的就是每个线程绑定自己的值,可以将ThreadLocal类比喻成全局存放数据的盒子,盒子中可以存储每个线程的私有数据。ThreadLocal解决的是变量在不同线程的隔离性,也就是不同线程拥有自己的值,不同线程中的值是可以放入ThreadLocal类中进行保存的。

package org.github.lujiango;

public class Test03 {
    public static ThreadLocal

  第一次调用ThreadLocal类的get()方法返回值是null,可以自定义protected T initialValue()方法,设置默认值。

package org.github.lujiango;

class ThreadLocalExt  extends ThreadLocal {
    @Override
    protected String initialValue() {
        return "defalut" ;
    }
}

public class Test03 {
    public static ThreadLocalExt tl =  new ThreadLocalExt();

    static class ThreadA  extends Thread {
        @Override
        public void run() {
            try {
                System.out.println( "ThreadA get value = " + tl.get());
                tl.set( "ThreadA" );
                Thread.sleep( 200 );
                System.out.println( "ThreadA get value = " + tl.get());

            }  catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    static class ThreadB  extends Thread {
        @Override
        public void run() {
            try {
                System.out.println( "ThreadB get value = " + tl.get());
                tl.set( "ThreadB" );
                Thread.sleep( 200 );
                System.out.println( "ThreadB get value = " + tl.get());

            }  catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            ThreadA a =  new ThreadA();
            ThreadB b =  new ThreadB();
            a.start();
            b.start();
        }  catch (Exception e) {
            e.printStackTrace();
        }
    }

}

InheritableThreadLocal

InheritableThreadLocal可以在子线程中取得父线程继承下来的值,即可以让子线程从父进程中取得值。

 

package org.github.lujiango;

public class Test04 {
    static InheritableThreadLocal itl =  new InheritableThreadLocal();

    static class ThreadA  extends Thread {
        @Override
        public void run() {
            try {
                System.out.println( "ThreadA get: " + itl.get());
            }  catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

        try {
            itl.set( "Main" );
            Thread.sleep( 1000 );
            ThreadA a =  new ThreadA();
            a.start();
        }  catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

InheritableThreadLocal即可以设置默认值,也可以在继承父线程值的同时修改。

 

package org.github.lujiango;

class InheritableThreadLocalExt  extends InheritableThreadLocal {
    @Override
    protected String initialValue() {
        return "defalut" ;
    }

    @Override
    protected String childValue(String parentValue) {
        return parentValue +  " child" ;
    }
}

public class Test04 {
    static InheritableThreadLocalExt itl =  new InheritableThreadLocalExt();

    static class ThreadA  extends Thread {
        @Override
        public void run() {
            try {
                Thread.sleep( 1000 );
                System.out.println( "ThreadA get: " + itl.get());
            }  catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

        try {
            System.out.println(itl.get());
            itl.set( "Main1" );
            Thread.sleep( 1000 );
            ThreadA a =  new ThreadA();
            a.start();
            itl.set( "Main2" );
            System.out.println(itl.get());
        }  catch (Exception e) {
            e.printStackTrace();
        }
    }

}

注:如果子线程在取得值得同时,主线程将InheritableThreadLocal中的值进行更改,那么子线程取到的值还是旧值。

 

posted @ 2017-09-23 12:11  小路不懂2  阅读(215)  评论(0编辑  收藏  举报