码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA线程间共享变量

时间:2014-06-26 07:02:37      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   java   http   tar   

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

public class ThreadScopeShareData {


	static Map<Thread, Integer> dataMap = 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 data = new Random().nextInt(100);

					dataMap.put(Thread.currentThread(), new Integer(data));

					System.out.println("put the " + data + " into :"
							+ Thread.currentThread());

					new A().get();

					new B().get();

				}
			}).start();
		}

	}

	static class A {
		public void get() {
			int data = (int) dataMap.get(Thread.currentThread());
			System.out.println("A has got the data :" + data);
		}
	}

	static class B {
		public void get() {
			int data = (int) dataMap.get(Thread.currentThread());
			System.out.println("B has got the data :" + data);
		}
	}

}

bubuko.com,布布扣更多内容请关注公众号

JAVA线程间共享变量,布布扣,bubuko.com

JAVA线程间共享变量

标签:class   blog   code   java   http   tar   

原文地址:http://blog.csdn.net/superstonne/article/details/34507581

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!