Facebook

adsense

Saturday, 2 May 2015

QUESTION 11

What will happen when you attempt to compile and run the following code?

public class MyThread extends Thread
{
    String myName;
    MyThread(String name)
    {
        myName = name;
    }
    public void run()
    {
        for(int i=0; i<100;i++)
        {
            System.out.println(myName);
        }
    }
    public static void main(String args[])
    {
        try
        {
            MyThread mt1 = new MyThread("mt1");
            MyThread mt2 = new MyThread("mt2");
            mt1.start();
            // XXX
            mt2.start();
        }
        catch(InterruptedException ex)
        {
        }
    }
}
----------------------------------------------------------------------------------
A. The above code in its current condition will not compile. 
B. In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.join(); can be placed at //XXX position.
C. In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.sleep(100); can be placed at //XXX position.
D. In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.run(); can be placed at //XXX position.
E. In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), there is no need to write any code.

<PREVIOUS || Main Page || NEXT >
GIVE YOUR ANSWER IN THE COMMENT BELOW

No comments:

Post a Comment