aspx网站做app/网络推广app是违法的吗
以前用ThreadLocal用的比较少,今天在看以前的代码的时候,发现有个类里面的方法里面使用了公共的静态变量,而且有很多类都引用了这个方法。由于这个变量不是final的,是动态赋值的,所以在多线程的情况下可能会引起问题。因为要改造这个方法的这个变量问题,所以我想到了ThreadLocal,并对ThreadLocal进行了一次实验,通过实验,对ThreadLocal有了一次比较深刻的认识。在此把代码贴出来,跟各位共同学习一下。
public class Test {public static ThreadLocal<String> threadLocal = new ThreadLocal<String>();private static final long waitTime = 3000L;public static void main(String[] args) throws Exception {Thread.sleep(60000);Thread thread1 = new Thread(new Runnable() {@Overridepublic void run() {System.out.println(Thread.currentThread().getName());threadLocal.set(Thread.currentThread().getName()+"----111111111");try {Thread.sleep(waitTime);} catch (InterruptedException e) {e.printStackTrace();}User user = new User("张三");user.say();}});Thread thread2 = new Thread(new Runnable() {@Overridepublic void run() {System.out.println(Thread.currentThread().getName());threadLocal.set(Thread.currentThread().getName()+"----22222222");try {Thread.sleep(waitTime);} catch (InterruptedException e) {e.printStackTrace();}User user = new User("李四");user.say();}});Thread thread3 = new Thread(new Runnable() {@Overridepublic void run() {System.out.println(Thread.currentThread().getName());threadLocal.set(Thread.currentThread().getName()+"----3333333333");try {Thread.sleep(waitTime);} catch (InterruptedException e) {e.printStackTrace();}User user1 = new User("王五");user1.say();User user2 = new User("赵六");user2.say();}});thread1.start();thread2.start();thread3.start();Thread thread4 = new Thread(new Runnable() {@Overridepublic void run() {System.out.println(Thread.currentThread().getName());threadLocal.set(Thread.currentThread().getName()+"----4444444444");try {Thread.sleep(waitTime);} catch (InterruptedException e) {e.printStackTrace();}new Thread(new Runnable() {@Overridepublic void run() {User user1 = new User("宋八1");user1.say();threadLocal.set(Thread.currentThread().getName()+"----5555555555");User user2 = new User("宋八2");user2.say();}}).start();new Thread(new Runnable() {@Overridepublic void run() {User user1 = new User("钱九1");user1.say();threadLocal.set(Thread.currentThread().getName()+"----666666666666666");User user2 = new User("钱九2");user2.say();}}).start();}});thread4.start();Thread.sleep(60000);}
}
public class User {private String name;public User(){super();}public User(String name){this.name = name;}public void say(){System.out.println(name + " say:" + Test.threadLocal.get());}
}
结果输出如下:
Thread-2
Thread-3
Thread-4
Thread-5
张三 say:Thread-2----111111111
李四 say:Thread-3----22222222
王五 say:Thread-4----3333333333
赵六 say:Thread-4----3333333333
钱九1 say:null
钱九2 say:Thread-7----666666666666666
宋八1 say:null
宋八2 say:Thread-6----5555555555
做一下说明:
我创建了四个主线程,每个线程内部对ThreadLocal变量进行赋值,并在主线程内创建User对象,User对象会输出ThreadLocal的值。通过结果可以看出:
1、各个线程内部的ThreadLocal是不相互影响的,并且主线程与子线程是不相互影响的。
2、同一个线程内容,不同对象获取的同一个ThreadLocal对象是一样的。
3、子线程是无法获取到主线程的ThreadLocal值的
通过以上试验,我还进行一下内存的监测,发现一个线程调用结束之后,会进行对象的回收。如图:
通过
以上的试验,希望对大家理解和使用ThreadLocal能有所帮助。