Saturday, May 11, 2019

what will be the output of thread code?

Problem statement:
what will be the output of the following code?
  1. public class ExecutionFourteen extends Thread {
  2.     private int count;
  3.     private ExecutionFourteen(int count) {
  4.         this.count = count;
  5.     }
  6.     @Override
  7.     public void run() {
  8.         for (int i = 1; i < 4; i++) {
  9.             count = count + 1;
  10.             System.out.println("count inside run() method: " + count + ", thread name: " + Thread.currentThread().getName());
  11.         }
  12.     }
  13.     public static void main(String[] args) {
  14.         ExecutionFourteen t = new ExecutionFourteen(10);
  15.         t.start();
  16.         System.out.println("count inside main() method: " + t.count);
  17.     }
  18. }
Output:
count inside main() method: 10
count inside run() method: 11, thread name: Thread-0
count inside run() method: 12, thread name: Thread-0
count inside run() method: 13, thread name: Thread-0

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...