该类提供了线程局部 (thread-local) 变量。这些变量不同于它们的普通对应物,因为访问某个变量(通过其 get 或 set 方法)的每个线程都有自己的局部变量,它独立于变量的初始化副本。ThreadLocal 实例通常是类中的 private static 字段,它们希望将状态与某一个线程(例如,用户 ID 或事务 ID)相关联。看的不是很懂,
网上的例子也找不到
特来请教
给个小例子就行了
网上的例子也找不到
特来请教
给个小例子就行了
写2个线程,一个线程将ThreadLocal的值改变一样。
在另一个线程里面,你读取出来,你发现,线程1所做的修改仅影响到线程1,并没有影响到线程2ThreadLocal里面的值。
而且每个线程都不共享的map
public class MyNumber {
//使用ThreadLocal 维护唯一的变量
private static ThreadLocal<Integer> number = new ThreadLocal<Integer>() {
//去看ThreadLcoal的源代码,重写initialVal()方法,否则返回null
protected Integer initialValue() {
return 0;
}
} //拿到下一个号码
public Integer getNext() {
//ThreadLocal变量在调用set或者是get方法的时候,才回去调用initialValue() 且调用一次
number.set(number.get()+1);
return number.get();
} public static void main(String[] args) {
MyNumber mynumber = new MyNumber();
//这里 传入的 mynumber 是同一个对象 但是打印的结果却是相互不影响
//因为ThreadLocal保证了每个访问该变量的线程都有一个独立的副本
MyThread mt1 = new MyThread(mynumber);
MyThread mt2 = new MyThread(mynumber); mt1.start();
mt2.start();
}
}class MyThread implements Runnable{
private MyNumber my_number;
public MyThread(MyNumber my_number) {
this.my_number = my_number;
} public void run() {
for(int i=0;i<5;i++) {
syso(Thread.currentThread.getName+"-number:"+my_number.getNext());
}
}
}//以上就是一个小例子,如果有什么不懂,可以留言给我
public class Test {
public static void main(String[] args) {
Thread[] t = new Thread[3];
for (int i=0; i<3; i++) {
t[i] = new Thread() {
public void run() {
for (int i=0; i<3; i++) {
System.out.printf("%s, id=%d, times=%s\n", getName(), SampleThreadLocal.getId(), i);
}
}
};
t[i].start();
}
}
}class SampleThreadLocal {
private static int count = 0;
private static final ThreadLocal <Integer> id =
new ThreadLocal <Integer> () {
protected Integer initialValue() {
return count++;
}
};
public static int getId() {
return id.get();
}
}
LZ可以看看,每个线程不管取多少次id,都不会发生变化
package threadlocaltest;public class MyNumber {
private static ThreadLocal<Integer> number = new ThreadLocal<Integer>() {
protected Integer initialValue() {
return 0;
}
}; public Integer getNext() {
number.set(number.get() + 1);
return number.get();
} public static void main(String[] args) {
MyNumber mynumber = new MyNumber(); MyThread mt1 = new MyThread(mynumber);
MyThread mt2 = new MyThread(mynumber); mt1.start();
mt2.start();
}
}class MyThread extends Thread {
private MyNumber my_number; public MyThread(MyNumber my_number) {
this.my_number = my_number;
} public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "-number:"
+ my_number.getNext());
}
}
}package threadlocaltest;public class MyNumber1 {
private static Integer number = 0; public Integer getNext() {
number++;
return number;
} public static void main(String[] args) {
MyNumber1 mynumber = new MyNumber1(); MyThread1 mt1 = new MyThread1(mynumber);
MyThread1 mt2 = new MyThread1(mynumber); mt1.start();
mt2.start();
}
}class MyThread1 extends Thread {
private MyNumber1 my_number; public MyThread1(MyNumber1 my_number) {
this.my_number = my_number;
} public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "-number:"
+ my_number.getNext());
}
}
}
package threadlocaltest;public class Test1 {
public static void main(String[] args) {
Thread[] t = new Thread[3];
for (int i = 0; i < 3; i++) {
t[i] = new Thread() {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.printf("%s, id=%d, count1=%d, times=%s\n",
getName(), SampleThreadLocal.getId(),
SampleThreadLocal.getCount1(), i);
}
}
};
t[i].start();
}
}
}class SampleThreadLocal {
private static int count = 0;
private static int count1 = 0;
private static final ThreadLocal<Integer> id = new ThreadLocal<Integer>() {
protected Integer initialValue() {
return count++;
}
}; public static int getId() {
return id.get();
} public static int getCount1() {
return count1++;
}
}结果怎么这样的Thread-2, id=0, count1=0, times=0
Thread-0, id=1, count1=1, times=0
Thread-0, id=1, count1=3, times=1
Thread-0, id=1, count1=4, times=2
Thread-1, id=0, count1=0, times=0
Thread-1, id=0, count1=5, times=1
Thread-1, id=0, count1=6, times=2
Thread-2, id=0, count1=2, times=1
Thread-2, id=0, count1=7, times=2
initialValue()方法只调用一次
又修改了下,这样比较容易看了
package threadlocaltest;public class Test1 {
public static void main(String[] args) {
Thread[] t = new Thread[3];
for (int i = 0; i < 3; i++) {
t[i] = new Thread() {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.printf("%s, id=%d, count1=%d, times=%s\n",
getName(), SampleThreadLocal.getId(),
SampleThreadLocal.getCount1(), i);
}
}
};
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
t[i].start();
}
}
}class SampleThreadLocal {
private static int count = 0;
private static int count1 = 0;
private static final ThreadLocal<Integer> id = new ThreadLocal<Integer>() {
protected Integer initialValue() {
return count++;
}
}; public static int getId() {
return id.get();
} public static synchronized int getCount1() {
return count1++;
}
}
结果Thread-0, id=0, count1=0, times=0
Thread-0, id=0, count1=1, times=1
Thread-0, id=0, count1=2, times=2
Thread-1, id=1, count1=3, times=0
Thread-1, id=1, count1=4, times=1
Thread-1, id=1, count1=5, times=2
Thread-2, id=2, count1=6, times=0
Thread-2, id=2, count1=7, times=1
Thread-2, id=2, count1=8, times=2