Home > Articles

This chapter is from the book

Review Questions

3.1 Given the following code:

public class VTRQ1 {
  public static void main(String[] args) {
    Logger logger = Logger.getLogger("Test");
    Runnable r1 = () -> {
       int i = 0;
       while(true) {
         i++;
         logger.info(String.valueOf(i));
         try {
           Thread.sleep(1000);
         } catch (InterruptedException e) {
           break;
         }
       }
    };
    Thread t1 = Thread.ofPlatform().name("acme").unstarted(r1);
    t1.start();
    t1.interrupt();
  }
}

Which scenario is possible when running the program?

  1. Program will log nothing and continue to run indefinitely.

  2. Program will log the value of i and continue to run indefinitely.

  3. Program will log nothing and terminate.

  4. Program will log one or more values of i and terminate.

3.2 Given the following code:

public class VTRQ2 {
    public static void main(String[] args) {
    Logger logger = Logger.getLogger("Test");
    Runnable r1 = () -> {
      int i = 0;
      while(true) {
        i++;
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          if (Thread.currentThread().isInterrupted()) {
            break;
          }
          logger.info(String.valueOf(i));
        }
      }
      logger.info(String.valueOf(i));
    };
    Thread t1 = Thread.ofPlatform().name("acme").unstarted(r1);
    t1.start();
    t1.interrupt();
    }
  }

Which scenario is possible when running the program?

  1. Program will log nothing and continue to run indefinitely.

  2. Program will log the value of i and continue to run indefinitely.

  3. Program will log nothing and terminate.

  4. Program will log one or more values of i and terminate.

3.3 Given the following code:

public class VTRQ3 {
  public static void main(String[] args) {
    Logger logger = Logger.getLogger("Test");
    Runnable r1 = () -> {
      int i = 0;
      while(true) {
        i++;
        logger.info(String.valueOf(i));
        try {
           Thread.sleep(1000);
        } catch (InterruptedException e) {
           break;
        }
      }
    };
    Thread t1 = Thread.ofVirtual().name("acme").unstarted(r1);
    t1.start();
    t1.interrupt();
  }
}

Which scenarios are possible when running the program? Select the two correct answers.

  1. Program will log nothing and continue to run indefinitely.

  2. Program will log the value of i and continue to run indefinitely.

  3. Program will log nothing and terminate.

  4. Program will log one of more values of i and terminate.

3.4 Given the following code:

public class VTRQ4 {
  public static void main(String[] args) {
    Logger logger = Logger.getLogger("Test");
    Runnable r1 = () -> {
      int i = 0;
      while(true) {
        i++;
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          if (Thread.currentThread().isInterrupted()) {
            break;
          }
          logger.info(String.valueOf(i));
        }
      }
      logger.info(String.valueOf(i));
      };
      Thread t1 = Thread.ofVirtual().name("acme").unstarted(r1);
      t1.start();
      t1.interrupt();
    }
  }

Which scenarios are possible when running the program? Select the two correct answers.

  1. Program will log nothing and continue to run indefinitely.

  2. Program will log the value of i and continue to run indefinitely.

  3. Program will log nothing and terminate.

  4. Program will log one of more values of i and terminate.

3.5 Which statements are true about threads?

Select the two correct answers.

  1. The priority of a virtual thread cannot be changed.

  2. JVM only exits after all platform and virtual threads have completed their execution.

  3. When a virtual thread executes an I/O operation, it is blocked and its priority is set to 0.

  4. When a platform thread executes an I/O operation, it is blocked and its priority is set to 0.

  5. Virtual threads managed by a thread pool may improve application performance.

  6. Platform threads managed by a thread pool may improve application performance.

3.6 Which statements are true about virtual threads? Select the three correct answers.

  1. Virtual threads are managed by the JVM rather than the operating system.

  2. Virtual threads can significantly improve the performance of CPU-bound tasks.

  3. Existing concurrency codebases using platform threads require minimal refactoring in order to leverage the benefits of virtual threads.

  4. Virtual threads can increase the throughput of a concurrency application as they drastically reduce the overhead associated with platform threads.

  5. Virtual threads are designed to replace platform threads in all Java concurrency applications.

3.7 Which statements are true about threads? Select the two correct answers.

  1. A carrier thread is a virtual thread that is in the running state.

  2. A carrier thread is a platform thread on which a virtual thread is mounted for execution.

  3. A virtual thread has no name by default.

  4. An unmounted virtual thread when mounted to resume execution will continue running on the same platform thread.

3.8 Given the following code:

public class VTRQ8 {
  public static void main(String[] args) throws Exception {

    Runnable task = () -> System.out.printf("NAME: %s%n",
                                            Thread.currentThread().getName());
    // (1) Insert code here.
  }
}

Which options will cause the program to execute normally and always print NAME: vt_1 when inserted at (1)?

Select the two correct answers.

  1. Thread vt = Thread.startVirtualThread(task);
    vt.setName("vt_1");
    vt.start();
    vt.join();
  2. Thread vt = Thread.startVirtualThread(task);
    vt.setName("vt_1");
    vt.join();
  3. Thread.Builder.OfVirtual vtb = Thread.ofVirtual().name("vt_", 1);
    Thread vt = vtb.unstarted(task);
    vt = vtb.start(task);
    vt.join();
  4. Thread.Builder.OfVirtual vtb = Thread.ofVirtual();
    Thread vt = vtb.name("vt_", 1).started(task);
    vt.join();
  5. Thread.Builder.OfVirtual vtb = Thread.ofVirtual();
    Thread vt = vtb.unstarted(task).name("vt_", 1);
    vt.join();
  6. Thread.Builder.OfVirtual vtb = Thread.ofVirtual().name("vt_", 1);
    Thread vt = vtb.unstarted(task);
    vt.start(task);
    vt.join();
  7. Thread.Builder.OfVirtual vtb = Thread.ofVirtual().name("vt_", 1);
    Thread vt = vtb.unstarted(task);
    vt = vt.start();
    vt.join();
  8. Thread vt = Thread.ofVirtual().name("vt_", 1).start(task);
    vt.join();
  9. Thread.ofVirtual().name("vt_", 1).start(task).join();

3.9 Given the following code:

public class VTRQ9 {
public static void main(String[] args) throws Exception {

    Runnable task = () -> System.out.printf("NAME: %s%n",
                                            Thread.currentThread().getName());
    // (1) Insert code here.
  }
}

Which options will cause the program to execute normally and print NAME: vt_1 when inserted at (1)?

Select the two correct answers.

  1. ThreadFactory vtf = Thread.ofVirtual().name("vt_0").factory();
    Thread vt = vtf.newThread(task);
    vt.setName("vt_1");
    vt.start();
    vt.join();
  2. ThreadFactory vtf = Thread.ofVirtual().name("vt_0").factory();
    Thread vt = new Thread(task);
    vt.setName("vt_1");
    vt.start();
    vt.join();
  3. Thread vt = Thread.ofVirtual().name("vt_1").factory().newThread(task);
    vt.join();
  4. Thread vt = Thread.ofVirtual().name("vt_1").factory().newThread(task);
    vt.start().join();
  5. Thread.Builder.OfVirtual vtb = Thread.ofVirtual().name("vt_", 1);
    vtb.unstarted(task);
    Thread vt = vtb.factory().newThread(task);
    vt.start();
    vt.join();

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.