62,614
社区成员
发帖
与我相关
我的任务
分享
public class DoMain {
public static void main(String[] args) {
final A b = new A();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
b.str = new String();
}
});
thread.start();
}
}
class A{
public String str = new String();
}
public class DoMain {
public static void main(String[] args) {
new A();
}
}
class A{
{
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
str = new String("b");
System.out.println(Thread.currentThread().getName().concat(" ").concat(str));
}
});
thread.start();
}
public String str = new String("a");
{
System.out.println(Thread.currentThread().getName().concat(" ").concat(str));
}
}
main a
Thread-0 b
public class Test4 {
public static void main(String[] args) throws Exception {
TestThread test = new TestThread();
test.startThread();
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " --- " + test.getName());
}
}
class TestThread {
String name;
public void startThread() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
if (name == null) {
name = "Hello";
System.out.println(Thread.currentThread().getName() + " --- " + name);
}
}
});
t.start();
}
public String getName() {
return this.name;
}
}