【线程高新】——【ThreadLocal实现线程范围的共享变量】

先说明问题:先让每个线程都有自己线程范围内的共享变量

package com.xiaozhi.threadlocal;

import java.util.Random;

public class Test {

	private static int data=0; 
	public static void main(String[] args) {
		new Thread(){
			public void run() {
				data=new Random().nextInt();
				System.out.println(Thread.currentThread().getName()+":"+data);
				new A().get();
				new B().get();
			}
		}.start();
		
		new Thread(){
			public void run() {
				data=new Random().nextInt();
				System.out.println(Thread.currentThread().getName()+":"+data);
				new A().get();
				new B().get();
			}
		}.start();
	}
	
	static class A{
	
		public void get()
		{
			System.out.println(Thread.currentThread().getName()+"new A().get():"+data);
		}
	}
	static class B{
		
		public void get()
		{
			System.out.println(Thread.currentThread().getName()+"new B().get():"+data);
		}
	}
}


用Map实现每个线程都有自己线程范围内的共享变量

package com.xiaozhi.threadlocal;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Test1 {

	private static Map<Thread,Integer>map=new HashMap<Thread, Integer>();
	public static void main(String[] args) {
		new Thread(){
			public void run() {
				int data=new Random().nextInt();
				map.put(Thread.currentThread(),data);
				System.out.println(Thread.currentThread().getName()+":"+data);
				new A().get();
				new B().get();
			}
		}.start();
		
		new Thread(){
			public void run() {
				int data=new Random().nextInt();
				map.put(Thread.currentThread(),data);
				System.out.println(Thread.currentThread().getName()+":"+data);
				new A().get();
				new B().get();
			}
		}.start();
	}
	
	static class A{
	
		public void get()
		{
			System.out.println(Thread.currentThread().getName()+"--------------new A().get():"+map.get(Thread.currentThread()));
		}
	}
	static class B{
		
		public void get()
		{
			System.out.println(Thread.currentThread().getName()+"--------------new B().get():"+map.get(Thread.currentThread()));
		}
	}
}






ThreadLocal的底层就是Map


package com.xiaozhi.threadlocal;

import java.util.Random;

public class Test2 {

	private static ThreadLocal<Integer> threadLocal=new ThreadLocal<Integer>();
	public static void main(String[] args) {
		new Thread(){
			public void run() {
				int data=new Random().nextInt();
				threadLocal.set(data);
				System.out.println(Thread.currentThread().getName()+":"+data);
				new A().get();
				new B().get();
			}
		}.start();
		
		new Thread(){
			public void run() {
				int data=new Random().nextInt();
				threadLocal.set(data);
				System.out.println(Thread.currentThread().getName()+":"+data);
				new A().get();
				new B().get();
			}
		}.start();
	}
	
	static class A{
	
		public void get()
		{
			System.out.println(Thread.currentThread().getName()+"--------------new A().get():"+threadLocal.get());
		}
	}
	static class B{
		
		public void get()
		{
			System.out.println(Thread.currentThread().getName()+"--------------new B().get():"+threadLocal.get());
		}
	}
}





ThreadLocal装对象


package com.xiaozhi.threadlocal;

import java.util.Random;

public class Test3 {

	private static ThreadLocal<Person> threadLocal=new ThreadLocal<Person>();
	public static void main(String[] args) {
		new Thread(){
			public void run() {
				int data=new Random().nextInt();
				Person person=new Person();
				person.setName("xiaozhi"+data);
				person.setAge(data);
				threadLocal.set(person);
				System.out.println(Thread.currentThread().getName()+":"+data);
				new A().get();
				new B().get();
			}
		}.start();
		
		new Thread(){
			public void run() {
				int data=new Random().nextInt();
				Person person=new Person();
				person.setName("xiaozhi"+data);
				person.setAge(data);
				threadLocal.set(person);
				System.out.println(Thread.currentThread().getName()+":"+data);
				new A().get();
				new B().get();
			}
		}.start();
	}
	
	static class A{
	
		public void get()
		{
			System.out.println(Thread.currentThread().getName()+"--------------new A().get():"+threadLocal.get().getName()+":"+threadLocal.get().getAge());
		}
	}
	static class B{
		
		public void get()
		{
			System.out.println(Thread.currentThread().getName()+"--------------new B().get():"+threadLocal.get().getName()+":"+threadLocal.get().getAge());
		}
	}
}

class Person{
	private String name;
	private int age;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}



更优雅的代码


package com.xiaozhi.threadlocal2;

import java.util.Random;

public class Test4 {

	public static void main(String[] args) {
		new Thread(){
			public void run() {
				int data=new Random().nextInt();
				Person person=Person.getPersonInstance();
				person.setName("xiaozhi"+data);
				person.setAge(data);
				System.out.println(Thread.currentThread().getName()+":"+data);
				new A().get();
				new B().get();
			}
		}.start();
		
		new Thread(){
			public void run() {
				int data=new Random().nextInt();
				Person person=Person.getPersonInstance();
				person.setName("xiaozhi"+data);
				person.setAge(data);
				System.out.println(Thread.currentThread().getName()+":"+data);
				new A().get();
				new B().get();
			}
		}.start();
	}
	
	static class A{
	
		public void get()
		{
			System.out.println(Thread.currentThread().getName()+"--------------new A().get():"+ Person.getPersonInstance().getName()+":"+ Person.getPersonInstance().getAge());
		}
	}
	static class B{
		
		public void get()
		{
			System.out.println(Thread.currentThread().getName()+"--------------new B().get():"+ Person.getPersonInstance().getName()+":"+ Person.getPersonInstance().getAge());
		}
	}
}

class Person{
	private Person(){}
	private static ThreadLocal<Person> threadLocal=new ThreadLocal<Person>();
	public static Person getPersonInstance()
	{
		Person person=threadLocal.get();
		if(person==null)
		{
			person=new Person();
			threadLocal.set(person);
		}
		return person;
	}
	
	private String name;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值