黑马程序员-线程范围内的数据共享之ThreadLocal

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ---------------------

ThreadLocal

实现线程范围内的数据共享,一个ThreadLocal代表一个变量,故其中只能存放一个数据,若有多个数据都要实现线程范围内的数据共享,该怎么处理呢?

   此时就要将这多个变量封装到一个类中,然后将这个类存放到ThreadLocal中。

import java.util.Random;

public class ThreadLocalDemo {

	private static int data;
	private static ThreadLocal<Integer> tl = new ThreadLocal<Integer>();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int i = 0; i < 2; i++) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					Random random = new Random();
					synchronized (ThreadLocalDemo.class) {
						data = random.nextInt();
						System.out.println(Thread.currentThread().getName()
								+ "    " + data);
						tl.set(data);
					}
					System.out.println(Thread.currentThread().getName()
							+ "A    " + new ModuleA().getData());
					System.out.println(Thread.currentThread().getName()
							+ "B     " + new ModuleB().getData());
				}
			}).start();
		}

	}

	static class ModuleA {
		public int getData() {
			return tl.get();
		}
	}

	static class ModuleB {
		public int getData() {
			return tl.get();
		}
	}
}

线程范围内的多个变量的共享:

Person

public 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;
	}
}

ThreadLocalDemo

import java.util.Random;

public class ThreadLocalDemo {

	private static int data;
	private static ThreadLocal<Person> tl = new ThreadLocal<Person>();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int i = 0; i < 2; i++) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					Random random = new Random();
					synchronized (ThreadLocalDemo.class) {
						data = random.nextInt();
						System.out.println(Thread.currentThread().getName()
								+ "    " + data);
						Person person = new Person();
						person.setAge(data);
						person.setName(Thread.currentThread().getName() + data);
						tl.set(person);
					}
					System.out.println(Thread.currentThread().getName()
							+ "A    " + new ModuleA().getData());
					System.out.println(Thread.currentThread().getName()
							+ "B     " + new ModuleB().getData());
				}
			}).start();
		}

	}

	static class ModuleA {
		public String getData() {
			return tl.get().getName() + tl.get().getAge();
		}
	}

	static class ModuleB {
		public String getData() {
			return tl.get().getName() + tl.get().getAge();
		}
	}
}

但是这样做并不好,修改代码(这样做封装了ThreadLocal,使得外界看不到ThreadLocal):

Person

public class Person {

	static ThreadLocal<Person> tl = new ThreadLocal<Person>();
	private static Person 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;
	}

	private Person() {
	}

	public synchronized static Person getThreadInstance() {
		person = tl.get();
		if (person == null) {
			person = new Person();
			tl.set(person);
		}
		return person;
	}
}

ThreadLocalDemo

import java.util.Random;

public class ThreadLocalDemo {

	private static int data;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int i = 0; i < 2; i++) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					Random random = new Random();
					synchronized (ThreadLocalDemo.class) {
						data = random.nextInt();
						System.out.println(Thread.currentThread().getName()
								+ "    " + data);
						Person person = Person.getThreadInstance();
						person.setAge(data);
						person.setName(Thread.currentThread().getName() + data);
						Person.tl.set(person);
					}
					System.out.println(Thread.currentThread().getName()
							+ "A    " + new ModuleA().getData());
					System.out.println(Thread.currentThread().getName()
							+ "B     " + new ModuleB().getData());
				}
			}).start();
		}

	}

	static class ModuleA {
		public String getData() {
			return Person.tl.get().getName() + Person.tl.get().getAge();
		}
	}

	static class ModuleB {
		public String getData() {
			return Person.tl.get().getName() + Person.tl.get().getAge();
		}
	}
}


---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值