Menu
Topics Index
...
`

Execute Thread


If you need explanation Read this topic

If you need Answer Take test on this topic

Execute Thread
What will be the output of the following program?
class T implements Runnable {
    private static int i = 0;
    public void run() {
        System.out.print(++i + "~");
    }
}
public class TClass {
    public static void main(String args[]) throws Exception {
        Thread thread4 = new Thread(new T());
        Thread thread5 = new Thread(null, thread4, "Thread5", 5);
        thread4.run();
        thread5.run();
    }
}

A. 1~2~
B. 1~2~3~4~5~
C. 1~2~3~4~5~6~
D. Some other output
E. Compilation Error or Runtime Error
Topic: What is Multitasking and Multithreading?

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 is A: first thread4 is called like normal method(run())  and print 1~ next thread5 is called with parameters as null as Threadgroup,thread4 as Runnable and String,long as other Params. so next run() is called and print 2~ bcoz here i is "static" so output will be 1~2~

Posted by Nagendra Reddy    2014-03-24 09:21:55


firstly both thread4 and thread5 are created on by one....and after thread4 cals for run() function....in run 1~ is printed ....I is a static variable so it remains same value for the run function of thread5...after thread5 called run() prints 2~
so final out put:
1~2~
here different constructers doesn't affect run()

Posted by Ananda Kumar    2014-03-24 11:28:27


Congratulations Nagendra Reddy. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-03-25 04:51:22


© meritcampus 2019

All Rights Reserved.

Open In App