以下のプログラムを実行していますが、何らかの理由で、run() メソッドに到達しているようには見えません。基本的に、スレッドの動作を見つけようとしています。以下の結果が得られます。
pqni392fr8dchsdmajglvuqsof
pqni392fr8dchsdmajglvuqsof has wake up
l79uho1tjtot7pcmk4vhh5t8qc
l79uho1tjtot7pcmk4vhh5t8qc has wake up
adfapus6g1fst56daimrudkgji
adfapus6g1fst56daimrudkgji has wake up
iqfo9knc99kcb622g36c77m62
iqfo9knc99kcb622g36c77m62 has wake up
67vdpghqit1a4iv3593451ps0a
67vdpghqit1a4iv3593451ps0a has wake up
ご覧のとおり、スレッドがスリープする必要がある run() メソッドに到達していません。何が問題なのですか? そして別の質問ですが、出力の最初の行は常に main() からのものであることに気付いたので、スレッドはプログラムの最初の実行から run() を実行できますか?
ありがとうございました。
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
class myThread implements Runnable {
@Override// method run is to be executed by a new thread
public void run() {
System.out.println("I am here");
int timeRandom = new Random().nextInt(50);
try {
String ThrName = Thread.currentThread().getName();
Thread.sleep(timeRandom);
System.out.println("Thread " + ThrName + " sleeping " + timeRandom);
} catch (InterruptedException ex) {
Logger.getLogger(myThread.class.getName()).log(Level.SEVERE, null, ex);
}
// throw new UnsupportedOperationException("Not supported yet.");
}
}
class myClass {
static int nthread = 5;
public static void main(String[] args) {
ExecutorService myService = Executors.newFixedThreadPool(nthread);
while (nthread != 0) {
Thread.currentThread().setName(new BigInteger(130, new SecureRandom()).toString(32));
System.out.println(Thread.currentThread().getName());
myService.submit(Thread.currentThread());
System.out.println(Thread.currentThread().getName() + " has wake up");
//
nthread -= 1;
}
myService.shutdown();
}
}