Topics Index

Thread Runnable2913

If you need explanation Read this topic

If you need Answer Take test on this topic

Thread Runnable2913
What will be the output of the following program?
public class ThreadWithRunnable {
    public static void main(String[] args) {
        CounterClass thread1 = new CounterClass();
        CounterClass thread2 = new CounterClass();
        CounterClass thread3 = new CounterClass();
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);;

A. Count = 0
Count = 1
Count = 2
B. Count = 2
Count = 1
Count = 0
C. Count = 2
Count = 2
Count = 2
D. Output can not be determined
E. Compilation Error or Runtime Error
Topic: Creation Of Threads In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

option D cannot be determined..
as any of the threads many process count.processing of threads dosent depends on the others

Posted by Sasikanth Sharma    2014-11-07 12:28:20

Ans is D,,\

Posted by Raju VarshnEy    2014-11-07 13:01:54

Answer is A ..In the main class we are creating three instances of CounterClass and we are calling startThread method on those instances sequentially......      Each instance of CounterClass is a Thread since CounterClass implements Runnable...Insude the startThread method,we have a variable count which is shared by all the instances of CounterClass....Inside the startThread Method,,we are creatinga new Thread and calling run method...Since run method does not start the execution on a new all the calls to startThread will be handled sequentially and since count variable is shared by all the instances...After the execution of startthread method by thread1,count is 0 since it is post increment ...After thread2's execution of startThread count is 1 and after the execution of thread3's startThread,count is printed as 2

Posted by Shashanka Mogaliraju    2014-11-07 13:24:39

ans is a. because we are calling three Functions one by one

Posted by Shubham Bansal    2014-11-07 14:02:10

Ans is A..Runnable is one way two create the wil helpful when you want to create Threads but already your class is extending some other class so you cant extend Thread class but using Runnable we can...we create Instance for class which is implemetning Runnable and pass this Obj to the Thread class Constructor than we call start() with thread class Object......we can call Event run() but if we call run() Multithreading Effect wont get...

here we create 3 Instances for the CounterClass and called the startMethod() every time we are passing Calling Obj to Thread class Constrcutor and calling the run that here threads wont get started Bcz we are not calling start() so that here we have only one every method called one by count is static than it is common to all three Objects...initially it wil print 0 than increament Bcz here we use post increment

Posted by Uday Kumar    2014-11-07 15:51:39


Posted by Vikas Tiyagi    2014-11-07 19:30:21


Posted by Urvashi Arya    2014-11-07 19:44:11

This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Urvashi Arya. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-11-10 01:25:05

© meritcampus 2019

All Rights Reserved.

Open In App