Topics Index

Thread Runnable

If you need explanation Read this topic

If you need Answer Take test on this topic

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

Ans:D , we have 3 threads, thread execution depends on JVM. Hence output can not be determined.

Posted by Indrasena Reddy    2014-10-16 08:39:08

ans is a.
as thread will execute one by one and count execution will have to be done after one another..

Posted by Shubham Bansal    2014-10-16 09:25:49

ans is D as any one of thread can be executed first it depends on thread priority

Posted by Ramya Reddy Addulla    2014-10-16 10:41:07

Ans is we have 2 classes Counter class implementing thr Runnable interface..
Runnable is an interface which wil help to create threads without extending the Thread class...Runnable has an method with name run() we shoud override it...

in the main class we create 3 Objects for the CounterClass and using thse oobjects we are calling startThread() in these method we are creating Object for the Thread class by passing the "this" here this means Current Object..means if we call startThread() with thread1 than "this" act lyk thread1,if we cal with thread2 than this wil be thread2. etc...

til now we create threads only..we didnt start the these start an particaular thread we should invkoke start(),internally it wil cal run()...
here count is static varible it wil common to all Objects
here processro wil give the time slice for the threads so that we cant determine the Output...

Posted by Uday Kumar    2014-10-16 11:36:06

ans a.
Thread encapsulates thread of execution
to create a new thread, ur program wil either extend thread or implement runnable interface.

runnable interface declares the run() method that is required for use with the thread class.

any class that implement runnable interface must define run method.

in this program countrclass defins run() method.

main method counter class object created.

by calling startThread method thread instantiated and object start() is called, the thread invoks run() method.

counter class use static int variable
each tim if run() invokes thn it prints value of that variable.
thn it incremnts by 1.

static variable initilized ly once.

here3 tim run invokes so sop prints count 0 1 2

Posted by Maheshwari Natarajan    2014-10-16 18:22:40

This dose is now closed and the winners are Indrasena Reddy,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Vicky Maduri. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-10-17 04:10:40

© meritcampus 2019

All Rights Reserved.

Open In App