How to do it...

Follow these steps to implement the example:

  1. Create a class named Task and specify the Runnable interface. Implement the run() method to write the message in the console during 100 seconds:
        public class Task implements Runnable { 

@Override
public void run() {

Date start, end;
start = new Date();
do {
System.out.printf("%s: tick ",
Thread.currentThread().getName());
end = new Date();
} while (end.getTime() - start.getTime() < 100000);
}
}
  1. Implement the Main class with the main() method. Create 10 Task objects to create 10 threads. Start them and wait for their finalization using the join() method:
        public class Main { 
public static void main(String[] args) {

Thread[] threads = new Thread[10];

for (int i=0; i<10; i++) {
Task task=new Task();
threads[i]=new Thread(task);
threads[i].start();
}

for (int i=0; i<10; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
  1. Open a console window and execute the JConsole application. It's included in the bin directory of the JDK-9 installation:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.199.163