ThreadLocal

 

public class TestThreadLocal {

 

ThreadLocal<String> myLocal = new ThreadLocal<String>();

 

String myName;

 

public String getName() {

return myName;

}

 

public void setName(String name) {

myName = name;

}

 

public static void main(String[] args) throws InterruptedException {

TestThreadLocal ooo = new TestThreadLocal();

Thread[] threadArray = new Thread[10];

for (int i = 0; i < 10; i++) {

 

Thread thi = new Thread(new ThreadWithoutThreadLocal(ooo), "thread" + i);

threadArray[i] = thi;

}

 

for (Thread th : threadArray) {

th.start();

}

 

Thread.sleep(3000);

System.out.println("-----------------------------------------------------------------");

 

for (int i = 0; i < 10; i++) {

 

Thread thi = new Thread(new ThreadWithThreadLocal(ooo), "thread" + i);

threadArray[i] = thi;

}

 

for (Thread th : threadArray) {

th.start();

}

}

 

}

 

class ThreadWithoutThreadLocal implements Runnable {

public TestThreadLocal ooo;

 

public ThreadWithoutThreadLocal(TestThreadLocal o) {

ooo = o;

}

 

@Override

public void run() {

String localName = Thread.currentThread().getName();

ooo.setName(localName);

//ooo.myLocal.set(localName);

// it will be mess up if multi thread running

// result like this

// ThreadName =======thread1 but it's actual name thread1

// ThreadName =======thread2 but it's actual name thread2

// ThreadName =======thread1 but it's actual name thread0

// ThreadName =======thread4 but it's actual name thread4

// ThreadName =======thread3 but it's actual name thread3

// ThreadName =======thread7 but it's actual name thread7

// ThreadName =======thread6 but it's actual name thread6

// ThreadName =======thread5 but it's actual name thread5

// ThreadName =======thread8 but it's actual name thread8

// ThreadName =======thread9 but it's actual name thread9

System.out.println(

"ThreadName =======" + ooo.getName() + " but it's actual name  " + Thread.currentThread().getName());

 

}

 

}

 

class ThreadWithThreadLocal implements Runnable {

public TestThreadLocal ooo;

 

public ThreadWithThreadLocal(TestThreadLocal o) {

ooo = o;

}

 

@Override

public void run() {

String localName = Thread.currentThread().getName();

ooo.myLocal.set(Thread.currentThread().getName());

 

// ThreadName======thread0 but it's actual name thread0

// ThreadName======thread2 but it's actual name thread2

// ThreadName======thread1 but it's actual name thread1

// ThreadName======thread3 but it's actual name thread3

// ThreadName======thread5 but it's actual name thread5

// ThreadName======thread4 but it's actual name thread4

// ThreadName======thread8 but it's actual name thread8

// ThreadName======thread6 but it's actual name thread6

// ThreadName======thread9 but it's actual name thread9

// ThreadName======thread7 but it's actual name thread7

 

System.out.println("ThreadName======" + ooo.myLocal.get() + " but it's actual name  " + localName);

 

}

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值