ThreadLocal实现线程范围内共享变量
不同的線程保存不同的數據
package com.gyj.student;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
- ThreadLocal实现线程范围内共享变量
- @author guoyajie
*/
public class Test5 {
private static int date = 0;
private static Map<Thread, Integer> threadDate = new HashMap<Thread, Integer>();
public static void main(String[] args) {
for(int i = 0 ;i <2; i++){
new Thread(new Runnable() {
@Override
public void run() {
int date= new Random().nextInt();
System.out.println(Thread.currentThread().getName() + "已经放入" + date);
threadDate.put(Thread.currentThread(), date);
new A().get();
new B().get();
}
}).start();
}
}
static class A{
public int get(){
int date = (int) threadDate.get(Thread.currentThread());
System.out.println("A get date" +Thread.currentThread().getName() + date);
return date;
}
}
static class B{
public int get(){
int date = (int) threadDate.get(Thread.currentThread());
System.out.println("B get date"+Thread.currentThread().getName() + date);
return date;
}
}
}
ThreadLocal代替Map
package com.gyj.student;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import com.gyj.student.Test5.A;
import com.gyj.student.Test5.B;
/**
- ThreadLocal()
- @author guoyajie
*/
public class Test6 {
private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();
private static int date = 0;
// private static Map<Thread, Integer> threadDate = new HashMap<Thread, Integer>();
public static void main(String[] args) {
for(int i = 0 ;i <2; i++){
new Thread(new Runnable() {
@Override
public void run() {
int date= new Random().nextInt();
System.out.println(Thread.currentThread().getName() + “已经放入” + date);
x.set(date);
new A().get();
new B().get();
}
}).start();
}
}
static class A{
public int get(){
int date = x.get();
System.out.println("A get date" +Thread.currentThread().getName() + date);
return date;
}
}
static class B{
public int get(){
int date = x.get();
System.out.println("B get date"+Thread.currentThread().getName() + date);
return date;
}
}
}
ThreadLocal保存多個對象
package com.gyj.student;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import com.gyj.student.Test5.A;
import com.gyj.student.Test5.B;
/**
- ThreadLocal()
- @author guoyajie
*/
public class Test6 {
private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();
private static ThreadLocal<MyThreadScopeDate> myThreadScopeDate = new ThreadLocal<MyThreadScopeDate>();
private static int date = 0;
// private static Map<Thread, Integer> threadDate = new HashMap<Thread, Integer>();
public static void main(String[] args) {
for(int i = 0 ;i <2; i++){
new Thread(new Runnable() {
@Override
public void run() {
int date= new Random().nextInt();
System.out.println(Thread.currentThread().getName() + “已经放入” + date);
x.set(date);
MyThreadScopeDate myDate = new MyThreadScopeDate();
myDate.setName(“name” + date);
myDate.setAge(date);
myThreadScopeDate.set(myDate);
new A().get();
new B().get();
}
}).start();
}
}
static class A{
public int get(){
int date = x.get();
System.out.println("A get date" +Thread.currentThread().getName() + date);
return date;
}
}
static class B{
public int get(){
int date = x.get();
System.out.println("B get date"+Thread.currentThread().getName() + date);
return date;
}
}
}
class MyThreadScopeDate{
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
單例 struts2的思想
package com.gyj.student;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import com.gyj.student.Test5.A;
import com.gyj.student.Test5.B;
/**
- ThreadLocal()
- @author guoyajie
*/
public class Test6 {
private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();
private static ThreadLocal<MyThreadScopeDate> myThreadScopeDate = new ThreadLocal<MyThreadScopeDate>();
private static int date = 0;
// private static Map<Thread, Integer> threadDate = new HashMap<Thread, Integer>();
public static void main(String[] args) {
for(int i = 0 ;i <2; i++){
new Thread(new Runnable() {
@Override
public void run() {
int date= new Random().nextInt();
System.out.println(Thread.currentThread().getName() + “已经放入” + date);
x.set(date);
MyThreadScopeDate myDate = MyThreadScopeDate.getInstance();
myDate.setName(“name” + date);
myDate.setAge(date);
new A().get();
new B().get();
}
}).start();
}
}
static class A{
public void get(){
System.out.println(Thread.currentThread().getName() + MyThreadScopeDate.getInstance().getAge() + "");
}
}
static class B{
public void get(){
System.out.println(Thread.currentThread().getName() + MyThreadScopeDate.getInstance().getAge() + "");
}
}
static class MyThreadScopeDate{
private MyThreadScopeDate(){}
private static MyThreadScopeDate instance = null;
private static ThreadLocal map = new ThreadLocal();
public static MyThreadScopeDate getInstance(){
MyThreadScopeDate instance = map.get();
if(instance == null){
instance = new MyThreadScopeDate();
map.set(instance);
}
return instance;
}
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
}
ThreadLocal不需要担心引用对象垃圾回收,自动回收