public class A implements Runnable{
StringBuffer str;
public A(StringBuffer str) {
this.str=str;
}
public static void main(String[] args) throws InterruptedException {
StringBuffer str=new StringBuffer();
new Thread(new A(str)).start();
Thread.sleep(10);
str.append("这是B线程赋值");
System.out.println(str);
}
@Override
public void run() {
str.append("你好,这是A线程赋值!");
}
}
/*结果:你好,这是A线程赋值!,这是B线程赋值*/
public class A implements Runnable{
StringBuffer str;
public A(StringBuffer str) {
this.str=str;
}
public static void main(String[] args) throws InterruptedException {
StringBuffer str=new StringBuffer();
str.append("这是B线程赋值");
new Thread(new A(str)).start();
Thread.sleep(10);
System.out.println(str);
}
@Override
public void run() {
str.append("你好,这是A线程赋值!");
}
}
/*结果:这是B线程赋值,--你好,这是A线程赋值!*/ |