Note that there are some explanatory texts on larger screens.

plurals
  1. POThread behaviour in Java
    text
    copied!<p>I am running the program below but for some reason, it dosen't look like I am getting to the run() method. Basically I am trying to spot the behaviour of threads.I am getting the result below :<br></p> <p>pqni392fr8dchsdmajglvuqsof<br> pqni392fr8dchsdmajglvuqsof has wake up <br> l79uho1tjtot7pcmk4vhh5t8qc <br> l79uho1tjtot7pcmk4vhh5t8qc has wake up <br> adfapus6g1fst56daimrudkgji<br> adfapus6g1fst56daimrudkgji has wake up <br> iqfo9knc99kcb622g36c77m62 <br> iqfo9knc99kcb622g36c77m62 has wake up <br> 67vdpghqit1a4iv3593451ps0a <br> 67vdpghqit1a4iv3593451ps0a has wake up <br></p> <p>As you see I am not getting to the run() method where a thread should sleep.what is the problem? And another question, could a thread execute the run() from the first run of the program because I noticed that the first line of the output is always from the main().</p> <p>Thank you.</p> <hr> <pre><code>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(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload