多线程(5)

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不需要担心引用对象垃圾回收,自动回收

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值