import java.util.Random;
public class ThreadScopeShareData {
/**
* @param args
*/
private static int data = 0;
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() {
data = new Random().nextInt();
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName()+"put data"+data);
new A().get();
new B().get();
}
}).start();
}
}
static class A{
public void get() {
// TODO Auto-generated method stub
System.out.println("A "+Thread.currentThread().getName()+" data "+data);
}
}
static class B{
public void get() {
// TODO Auto-generated method stub
System.out.println("B "+Thread.currentThread().getName()+"data "+data);
}
}
}
运行如下:
Thread-0 put data-1216341610
Thread-1 put data-1216341610
A Thread-0 data -1216341610
A Thread-1 data -1216341610
B Thread-0 data -1216341610
B Thread-1 data -1216341610
请问为什么thread -0 ,thread-1中 new Random().nextInt()的值是一样的,并没有随机产生;
|
|