【发布时间】:2012-07-12 03:35:33
【问题描述】:
我有一个名为 Test Example 的类,它有一个名为 dance() 的方法。在主线程中,如果我在子线程中调用 dance() 方法,会发生什么?我的意思是,该方法会在子线程还是主线程中执行?
public class TestExample {
public static void main(String[] args) {
final TestExample test = new TestExample();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hi Child Thread");
test.dance();
}
}).start();
}
public void dance() {
System.out.println("Hi Main thraed");
}
}
【问题讨论】:
-
尝试将
System.out.format("thread: %s\n", Thread.currentThread().getName());放入main和run。你会得到你的答案。
标签: java multithreading