Thursday, September 26, 2019

Multithreading In Java

Multithreading In Java


Multithreading Questions:

1. What will be the output of the program?

class Test extends Thread {
public
    void run()
    {
        System.out.println("Run");
    }
} class Myclass {
public
    static void main(String[] args)
    {
        Test t = new Test();
        t.start();
    }
}


Options:
1. One thread created
2. Two thread created
3. Depend upon system
4. No thread created

Output:
The answer is option (2)

Explanation : In the above program, one thread will be created i.e. the main thread which is responsible to execute the main() method and the child thread will be created after the execution of t.start() which is responsible to execute run() method.

Question: Output of below program

class Test extends Thread {
public
void run()
{
System.out.println("Run");
}
} class Myclass {
public
static void main(String[] args)
{
Test t = new Test();
t.run();
}
}

Options:
1. One thread created
2. Two thread created
3. Depend upon system
4. No thread created
Output:
 The answer is option (1)
Explanation : In the above program only one thread will be created i.e. the main thread which is responsible to execute the main() method only. The run() method is called by the object t like a normal method.
3. What will be the order of output of the program?

class Test extends Thread {
public
    void run()
    {
        System.out.println("Run");
    }
} class Myclass {
public
    static void main(String[] args)
    {
        Test t = new Test();
        t.start();
        System.out.println("Main");
    }
}
Options:
1. Main Run
2. Run Main
3. Depend upon Program
4. Depend upon JVM
Output:
 The answer is option (4)
Explanation : In the above program, we cant predict the exact order of the output as it is decided by the Thread scheduler which is the part of JVM.









No comments:

Post a Comment

Create a Digital Clock using HTML and JavaScript

Create a Digital Clock using HTML and JavaScript  <! DOCTYPE html> < html > < head > <...

Followers

Search This Blog

Popular Posts