マルチスレッド プログラムの実行中に現在のスレッドが何であるかについて混乱しています。
public class CurrentThread {
public static void main(String[] args) {
// FROM HERE: will always be "main-thread" the current thread ?
CurrentThread currentThread = new CurrentThread();
currentThread.testCurrentThread();
// TO HERE
}
private void testCurrentThread() {
// some other threads starts...
AThread athread = new AThread();
athread.start();
// some other threads starts...
}
class AThread extends Thread {
public AThread() {
setName("thread-a");
}
public void run() {
// FROM HERE: will always be thread-a the current thread during finish the run method ?
// some process
// TO HERE...
}
}
}
スレッド AThread の開始前後に複数のスレッドを起動するとします。
- メイン メソッド内にいる場合、Thread.currentThread() を呼び出すたびに「メイン スレッド」になりますか?
- AThread の run メソッド内にいる場合、Thread.currentThread() を呼び出すたびに「a-thread」になりますか?
前もって感謝します。