O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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\n",                                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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required