62,635
社区成员




public class ThreadMain {
Map<String,A> m = new HashMap<String, A>();
void putA(A a){
m.put("k", a);
}
void main(A a) {
int i = 10;
while(i > 0) {
B b = new B();
b.setValue(i);
a.setB(b);
R r = new R(m);
Thread t = new Thread(r);
t.start();
i--;
}
}
public static void main(String[] args) {
ThreadMain d = new ThreadMain();
A a = new A();
d.putA(a);
d.main(a);
}
}
class R implements Runnable{
Map<String,A> m;
R(Map<String,A> m){
this.m = m;
}
public void run() {
A a = m.get("k");
System.out.println("before set A.b.value is:"+a.getB().getValue());
B b = new B();
b.setValue(8888);
a.setB(b);
System.out.println("after set A.b.value is:"+a.getB().getValue());
}
}
class A {
private B b;
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
class B{
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
public class ThreadMain {
Map<String,A> m = new HashMap<String, A>();
void putA(A a){
m.put("k", a);
}
void main(A a) {
int i = 10;
while(i > 0) {
B b = new B();
b.setValue(i);
a.setB(b);
R r = new R(m);
Thread t = new Thread(r);
t.start();
i--;
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ThreadMain d = new ThreadMain();
A a = new A();
d.putA(a);
d.main(a);
}
}
class R implements Runnable{
Map<String,A> m;
R(Map<String,A> m){
this.m = m;
}
public void run() {
A a = m.get("k");
System.out.println("before set A.b.value is:"+a.getB().getValue());
B b = new B();
b.setValue(8888);
a.setB(b);
System.out.println("after set A.b.value is:"+a.getB().getValue());
}
}
class A {
private B b;
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
class B{
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
import java.util.HashMap;
import java.util.Map;
public class ThreadMain {
Map<String,A> m = new HashMap<String, A>();
void putA(A a){
m.put("k", a);
}
void main(A a) {
int i = 10;
while(i > 0) {
// B b = new B();
// b.setValue(i);
// a.setB(b);
R r = new R(m,i);
Thread t = new Thread(r);
t.start();
i--;
}
}
public static void main(String[] args) {
ThreadMain d = new ThreadMain();
A a = new A();
d.putA(a);
d.main(a);
}
}
class R implements Runnable{
Map<String,A> m;
int i;
R(Map<String,A> m,int i){
this.m = m;
this.i = i;
}
public void run() {
synchronized(ThreadMain.class){
A a = m.get("k");
B b = new B();
b.setValue(i);
a.setB(b);
System.out.println("before set A.b.value is:" + a.getB().getValue());
b = new B();
b.setValue(8888);
a.setB(b);
System.out.println("after set A.b.value is:" + a.getB().getValue());
}
}
}
class A {
private B b;
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
class B{
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}