Menu
Question Index
...

What will be the output of the following program?

public class ThreadWithRunnable {
    public static void main(String[] args) {
        CounterClass thread1 = new CounterClass();
        thread1.startThread();
        CounterClass thread2 = new CounterClass();
        thread2.startThread();
        CounterClass thread3 = new CounterClass();
        thread3.startThread();
    }
}
class CounterClass implements Runnable {
    Thread thread;
    public static int count = 0;
    public void run() {
        System.out.println("Count = " + count++);
    }
    public void startThread() {
        thread = new Thread(this);
        thread.start();
    }
}


Count = 0
Count = 1
Count = 2
Count = 2
Count = 1
Count = 0
Count = 2
Count = 2
Count = 2
Output can not be determined
Compilation Error or Runtime Error

Doubts

Problems

Topic: Creation Of Threads In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App