Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. [15] Consider the following code: public void printNumbers(int start, int end

ID: 3768323 • Letter: 5

Question

5. [15] Consider the following code:

public void printNumbers(int start, int end) {

for (int i = start; i < end; ++i)

System.out.println(i);

}

public void printNumbersThreaded() {

ExecutorService executor = Executors.newCachedThreadPool();

executor.execute(new Runnable() {

public void run() { printNumbers(1, 5);}

});

executor.execute(new Runnable() {

public void run() { printNumbers(6, 9); }

});

executor.shutdown();

}

a. (5) List all of the numbers that will be printed when the above code is run (independent of ordering):

b. (5) Can we guarantee that the numbers will be printed in strictly increasing order? Why or why not?

c. (5) If the printNumbers method is synchronized (i.e., defined using the synchronized keyword), can we guarantee that the numbers will be printed in strictly increasing order? Why or why not?

Explanation / Answer

Program is :

package com.urbanpro.nancy.solutions;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadTest {

   public void printNumbers(int start, int end) {
       for (int i = start; i < end; ++i)
           System.out.println(i);
   }

   public void printNumbersThreaded() {
       ExecutorService executor = Executors.newCachedThreadPool();
       executor.execute(new Runnable() {
           public void run() {
               printNumbers(1, 5);
           }
       });
       executor.execute(new Runnable() {
           public void run() {
               printNumbers(6, 9);
           }
       });
       executor.shutdown();
   }

   public static void main(String[] args) {
       ThreadTest obj = new ThreadTest();
       obj.printNumbersThreaded();
   }
}

Output:

1
2
3
4
6
7
8

Executors.execute() does not make any ordering guarantees. Workers will take from the queue in order, but may be context-switched at any time before completion.You have no guarantees about which order the Runnables are executed.ExecutorService is an interface that does not mandate a particular way how the submitted tasks (not threads) are executed.So the answer in NO .

The Concurrency API introduces the concept of an ExecutorService as a higher level replacement for working with threads directly. Executors are capable of running asynchronous tasks and typically manage a pool of threads, so we don't have to create new threads manually. All threads of the internal pool will be reused under the hood for revenant tasks, so we can run as many concurrent tasks as we want throughout the life-cycle of our application with a single executor service.

Answer c: yes , You need to have synchronous blocks, in case of asynchronous processing.

The Java synchronized Keyword. Synchronized blocks in Java are marked with the synchronized keyword. A synchronized block inJava is synchronized on some object. All synchronized blockssynchronized on the same object can only have one thread executing inside them at the same time.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote