我有一个名为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( “线程:%S \ n” 个,Thread.currentThread()的getName()); 'main'和'run'里面。你会得到你的答案。 – alphazero 2012-07-12 03:41:19