June 2018
Beginner
722 pages
18h 47m
English
Let's use our new class, AThread, to demonstrate the behavior we have described. Here is the code we are going to run first:
Thread thr1 = new AThread(1, 4);thr1.start();Thread thr2 = new AThread(11, 14);thr2.setDaemon(true);thr2.start();try { TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) { e.printStackTrace();}System.out.println("Main thread exists");
In the preceding code, we create and immediately start two threads – a user thread, thr1, and a daemon thread, thr2. Actually, there is a user thread called main too, so we run two user threads and one daemon thread. Each of the child threads is going to print the incremented number four times, pausing for 1 second after each print. This means ...
Read now
Unlock full access