Java Multithreading using Executors Framework

Arun K., Technical Architect and Software Engineer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
10 Lessons (1h)
    • 1. Creating Threads Using the Threads-API | First Way

      10:25
    • 2. Creating Threads Using the Threads-API | Second Way

      4:27
    • 3. Creating Threads Using the Threads-API | Third Way

      4:32
    • 4. Creating Threads Using the Threads-API | Fourth Way

      3:50
    • 5. Creating Threads Using the Threads-API | Fifth Way

      3:54
    • 6. Executors Framework Overview

      4:05
    • 7. Important Interfaces and Classes in the Executors Framework

      10:36
    • 8. Creating Threads Using Executors Framework - Fixed Thread Pool

      10:32
    • 9. Creating Threads Using Executors Framework - Single Thread Executor

      3:09
    • 10. Creating Threads Using Executors Framework - Cached Thread Pool | Part - 1

      5:27

Project Description

Learn how to use the Executors to do everything that you can do with the Threads API ... and more

This is a code-along type of class. The source code zip of all the examples developed in various sessions can be downloaded here. You (the student!) are expected to try coding the examples yourself too, for practice, after watching the tutorials.

For the project to be completed and submitted by the students, there is a set of seven questions that you have to answer. Most of them are coding exercises and some are subjective ones. Once you have completed an exercise, you need to upload its source code to the project gallery. You also need to submit your subjective understanding/interpretation whereever asked in the questions.

All the best!

PROJECT QUESTIONS

  1. Let's do some thread-pool tuning! We will use CalculationTaskC as the task; but first update its call() method so that the 'for' loop inside it runs only for 10 times instead of 1000 and update the rest of the code accordingly. Now, create an ExecutorService and submit 1000 instances of the modified version of CalculationTaskC to it.

    (a) As ExecutorService, use a fixed-thread-pool of size 3. Note down the no. of threads and the time taken to execute all the tasks.
    (b) Increase the pool size by 3 and run again. Note down the no. of threads and the time taken to execute all the tasks.
    (c) Repeat (b). You will notice that the execution-time for executing all the 1000 tasks decreases a bit i.e. your throughput has increased! Continue repeating (b). At some point the execution-time will start increasing. Stop at this point!
    (d) Post the statistics in the following format:

    ======================================
    # OF THREADS                         TIME TAKEN
    ======================================
              3                                       <25.543 s>
              6                                       <18.879 s>
              9                                       <12.799 s>
              .                                               .
              .                                               .
             n                                               t
    ======================================

    Laptop Name/Model :
    Processor Make        :
    # of cores                   :
    RAM                            :

  2. Repeat Exercise-1 but use a cached-thread-pool instead. Obviously you will need to run it just once! Do note down the maximum no. threads created by the cached-thread-pool internally and the total time taken to execute all the tasks. Post the statistics too in the same format as Exercise-1.

  3. Repeat Exercise-1 (with fixed-thread-pool only). This time vary the load - submit 5000 tasks. Post the statistics too.

  4. Repeat Exercise-3 but use a cached-thread-pool (load remains the same i.e. 5000 tasks). Post the statistics too.

  5. What are your observations and inferences from the data that you have collected for Exercises 1 to 4?

  6. This exercise concerns naming an Executor thread that executes only one task for the whole of its life. I want to create a fixed-thread-pool with three threads. Each of these three threads will execute only one of the following tasks for the whole of its lifetime:

    - Thread-1 => Monitor a specific file for updates.
    - Thread-2 => Wait for and read the input from a specific port.
    - Thread-3 => Animate an object on the screen at random intervals

    What is the best way to name these threads?

  7. You have seen many examples of scheduling tasks for repeated executions in the course. All those tasks were Runnables where one thread was scheduling the tasks while another thread was actually executing them. Now, for this exercise, you need to code so that you can "return" a value from your task to the calling thread; and that too - EVERY TIME it runs. Write using:

    (a) The Threads API
    (b) The Executors Framework

Student Projects