标签:getname hashmap import add ble 集合 dem sys string
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class ThreadDemo2 {
static int data = 0; //定义一个数据
static Map<Thread, Integer> map=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() {
//data = (int) Math.random() * 10;
int data =new Random().nextInt();
map.put(Thread.currentThread(), data);
System.out.println(Thread.currentThread().getName() + " data:" + data);
new a().get();
new b().get();
}
}).start();
}
}
static class a {
public void get() {
int data=map.get(Thread.currentThread());
System.out.println(Thread.currentThread().getName() + " a data:" + data);
}
}
static class b {
public void get() {
int data=map.get(Thread.currentThread());
System.out.println(Thread.currentThread().getName() + " b data:" + data);
}
}
}
标签:getname hashmap import add ble 集合 dem sys string
原文地址:http://www.cnblogs.com/yugeng/p/7854291.html