类操作自己的静态变量引发的线程安全问题
今日看了一个关于线程安全的问题,调用关系如下。 移至回收站
main–>classA.methodA–>methodA–>a;
其中a为静态变量,因为静态变量是在堆中声明的,堆变量线程间共享,所以可能存在不同线程对同一内存地址进行操作,引发线程安全问题。
外部调用的classA每次内存地址都不同,无论classA的变量为基础数据类型还是对象类型,如果声明为静态变量都会引发线程安全问题。所以可以理解为静态变量存储在堆中供多线程间直接调用,当多线程时,自身实例调用了其他实例的堆内存中的值引发线程安全问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.hl.thread; /** * Create by hanlin on 2018年1月17日 **/ public class StaticTest { public static void main(String[] args) throws InterruptedException { for (int i = 0; i <100; i++){ Thread t = new Thread(new Runnable() { @Override public void run() { Bean b = new Bean(); b.name(); } }); t.setName("Thread"+i); t.start(); } Thread.sleep(10000); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.hl.thread; /** * Create by hanlin on 2018年1月17日 **/ public class Bean { private static Name n; public Bean() { super(); n = new Name(); n.setS(Thread.currentThread().getName()); } public void name() { if(Thread.currentThread().getName().equals(n.getS())){ System.out.println(String.format("ThreadName【%s】 name【%S】 n【%s】", Thread.currentThread().getName(),n.getS(),n)); }else{ System.err.println(String.format("ThreadName【%s】 name【%S】 n【%s】", Thread.currentThread().getName(),n.getS(),n)); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.hl.thread; /** * Create by hanlin on 2018年1月17日 **/ public class Name { private String s; public String getS() { return s; } public void setS(String s) { this.s = s; } } |
1 |
线程17操作了,线程19的变量a,内存地址为16d50866。 |
ThreadName【Thread53】 name【THREAD1】 n【com.hl.thread.Name@222d68d9】
ThreadName【Thread20】 name【THREAD20】 n【com.hl.thread.Name@7506f74a】
ThreadName【Thread50】 name【THREAD50】 n【com.hl.thread.Name@168339d3】
ThreadName【Thread17】 name【THREAD19】 n【com.hl.thread.Name@16d50866】
ThreadName【Thread9】 name【THREAD9】 n【com.hl.thread.Name@1e702253】
ThreadName【Thread34】 name【THREAD34】 n【com.hl.thread.Name@4cb3c017】
ThreadName【Thread55】 name【THREAD55】 n【com.hl.thread.Name@6cbbe4f1】
ThreadName【Thread6】 name【THREAD6】 n【com.hl.thread.Name@4e31adfb】
ThreadName【Thread76】 name【THREAD76】 n【com.hl.thread.Name@5109fd5c】
ThreadName【Thread95】 name【THREAD95】 n【com.hl.thread.Name@3295296d】
ThreadName【Thread15】 name【THREAD15】 n【com.hl.thread.Name@14f67d7d】
ThreadName【Thread72】 name【THREAD72】 n【com.hl.thread.Name@6ab58489】
ThreadName【Thread96】 name【THREAD96】 n【com.hl.thread.Name@4eeb11b2】
ThreadName【Thread7】 name【THREAD7】 n【com.hl.thread.Name@6426ac69】
ThreadName【Thread27】 name【THREAD27】 n【com.hl.thread.Name@664a66b6】
ThreadName【Thread75】 name【THREAD75】 n【com.hl.thread.Name@48590c31】
ThreadName【Thread14】 name【THREAD14】 n【com.hl.thread.Name@2bc2d139】
ThreadName【Thread12】 name【THREAD12】 n【com.hl.thread.Name@381037cd】
ThreadName【Thread16】 name【THREAD16】 n【com.hl.thread.Name@2516c93f】
ThreadName【Thread77】 name【THREAD77】 n【com.hl.thread.Name@566c4ab8】
ThreadName【Thread4】 name【THREAD4】 n【com.hl.thread.Name@12341ad6】
ThreadName【Thread0】 name【THREAD0】 n【com.hl.thread.Name@5e990133】
ThreadName【Thread11】 name【THREAD11】 n【com.hl.thread.Name@4a8f0963】
ThreadName【Thread8】 name【THREAD8】 n【com.hl.thread.Name@46cf8b7b】
ThreadName【Thread94】 name【THREAD94】 n【com.hl.thread.Name@4f78e58c】
ThreadName【Thread69】 name【THREAD69】 n【com.hl.thread.Name@b0545bc】
ThreadName【Thread13】 name【THREAD13】 n【com.hl.thread.Name@44ea4bd2】
ThreadName【Thread33】 name【THREAD33】 n【com.hl.thread.Name@798f3789】
ThreadName【Thread29】 name【THREAD29】 n【com.hl.thread.Name@76f723c1】
ThreadName【Thread37】 name【THREAD37】 n【com.hl.thread.Name@5b2bae1c】
ThreadName【Thread89】 name【THREAD89】 n【com.hl.thread.Name@4b2c1afc】
ThreadName【Thread93】 name【THREAD93】 n【com.hl.thread.Name@39494968】
ThreadName【Thread49】 name【THREAD49】 n【com.hl.thread.Name@5d3dfd65】
ThreadName【Thread78】 name【THREAD78】 n【com.hl.thread.Name@6b8cebb0】
ThreadName【Thread68】 name【THREAD68】 n【com.hl.thread.Name@a1a1c96】
ThreadName【Thread23】 name【THREAD23】 n【com.hl.thread.Name@4045ca95】
ThreadName【Thread66】 name【THREAD66】 n【com.hl.thread.Name@e9c9830】
ThreadName【Thread56】 name【THREAD56】 n【com.hl.thread.Name@65fa68d】
ThreadName【Thread63】 name【THREAD63】 n【com.hl.thread.Name@57871364】
ThreadName【Thread87】 name【THREAD87】 n【com.hl.thread.Name@69ab83b8】
ThreadName【Thread24】 name【THREAD24】 n【com.hl.thread.Name@5b719eed】
ThreadName【Thread83】 name【THREAD83】 n【com.hl.thread.Name@31c6e548】
ThreadName【Thread65】 name【THREAD65】 n【com.hl.thread.Name@c0647e4】
ThreadName【Thread54】 name【THREAD58】 n【com.hl.thread.Name@ea6323a】
ThreadName【Thread64】 name【THREAD64】 n【com.hl.thread.Name@1000426f】
ThreadName【Thread43】 name【THREAD43】 n【com.hl.thread.Name@3837011a】
ThreadName【Thread97】 name【THREAD97】 n【com.hl.thread.Name@39cb3918】
ThreadName【Thread73】 name【THREAD73】 n【com.hl.thread.Name@2aba21d2】
ThreadName【Thread41】 name【THREAD41】 n【com.hl.thread.Name@1cef17dc】
ThreadName【Thread5】 name【THREAD5】 n【com.hl.thread.Name@79a12505】
ThreadName【Thread45】 name【THREAD45】 n【com.hl.thread.Name@3900ca87】
ThreadName【Thread46】 name【THREAD46】 n【com.hl.thread.Name@3eacbed5】
ThreadName【Thread39】 name【THREAD39】 n【com.hl.thread.Name@652058f1】
ThreadName【Thread62】 name【THREAD62】 n【com.hl.thread.Name@51b5a5b1】
ThreadName【Thread99】 name【THREAD99】 n【com.hl.thread.Name@216d75b8】
ThreadName【Thread31】 name【THREAD31】 n【com.hl.thread.Name@5a07ae42】
ThreadName【Thread70】 name【THREAD70】 n【com.hl.thread.Name@2179f8b4】
ThreadName【Thread42】 name【THREAD42】 n【com.hl.thread.Name@17748af7】
ThreadName【Thread58】 name【THREAD58】 n【com.hl.thread.Name@ea6323a】
ThreadName【Thread40】 name【THREAD40】 n【com.hl.thread.Name@597af38e】
ThreadName【Thread61】 name【THREAD61】 n【com.hl.thread.Name@31b44839】
ThreadName【Thread74】 name【THREAD74】 n【com.hl.thread.Name@4b6697ab】
ThreadName【Thread36】 name【THREAD36】 n【com.hl.thread.Name@46d42cf2】
ThreadName【Thread3】 name【THREAD3】 n【com.hl.thread.Name@752a2b85】
ThreadName【Thread98】 name【THREAD98】 n【com.hl.thread.Name@60f3f46d】
ThreadName【Thread48】 name【THREAD48】 n【com.hl.thread.Name@56c2b49a】
ThreadName【Thread2】 name【THREAD2】 n【com.hl.thread.Name@61d88f91】
ThreadName【Thread32】 name【THREAD32】 n【com.hl.thread.Name@71c797b5】
ThreadName【Thread38】 name【THREAD38】 n【com.hl.thread.Name@65570b43】
ThreadName【Thread84】 name【THREAD84】 n【com.hl.thread.Name@7a9f3c61】
ThreadName【Thread71】 name【THREAD71】 n【com.hl.thread.Name@fea0f65】
ThreadName【Thread1】 name【THREAD1】 n【com.hl.thread.Name@222d68d9】
ThreadName【Thread88】 name【THREAD88】 n【com.hl.thread.Name@1de8803c】
ThreadName【Thread25】 name【THREAD29】 n【com.hl.thread.Name@ea6323a】
ThreadName【Thread90】 name【THREAD90】 n%9