June 2018
Beginner
722 pages
18h 47m
English
The second way to create a thread is to use a class that implements Runnable. Here is an example of such a class that has almost exactly the same functionality as class AThread:
public class ARunnable implements Runnable { int i1, i2; public ARunnable(int i1, int i2) { this.i1 = i1; this.i2 = i2; } public void run() { for (int i = i1; i <= i2; i++) { System.out.println("child thread " + i); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } }}
The only difference is that there is no isDaemon() method in the Runnable interface, so we cannot print whether the thread is daemon or not.
Read now
Unlock full access