package com.susan.thread;
import java.util.HashMap;
import java.util.Random;
/**
* 五、线程内的共享数据
*
* @author dahai
*
*/
public class ThreadScopeShareData {
private static int data = 0;
private static HashMap<Thread, Integer> map = new HashMap<Thread,Integer>();
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
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("A:"+Thread.currentThread().getName()+"data:" + map.get(Thread.currentThread()) );
}
}
static class B {
public void get() {
System.out.println("B:"+Thread.currentThread().getName()+"data:" + map.get(Thread.currentThread()));
}
}
}