Saturday, May 11, 2019

what will be the output of thread problem?

Problem statement:
what will be the output of the following code?
  1. public class Person extends Thread {
  2.     private int count;
  3.     private Person(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.         }
  11.     }
  12.     public static void main(String[] args) {
  13.         Person t = new Person(10);
  14.         t.start();
  15.         System.out.println(t.count);
  16.     }
  17. }
Output: 10
output will vary, will explain the reason soon

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...