Output of Java program | Set 16 (Threads)

19 May, 2017
Prerequisites: multi-threading in java , Life cycle and states of a thread , Joining threads in java , start() method in java1) What could be the output of the following program?
public class Test implements Runnable{    public void run()    {        System.out.printf("GFG ");        System.out.printf("Geeks ");    }    public static void main(String[] args)    {        Test obj = new Test();        Thread thread = new Thread(obj);                  thread.start();        System.out.printf("Geeks ");        try         {            thread.join();        }         catch (InterruptedException e)         {            e.printStackTrace();        }        System.out.println("for ");    }}
a) GFG Geeks Geeks forb) Geeks GFG Geeks forc) Either a or bd) Both a and b together
Ans. (c)Explanation: From the statement “thread.start()”, we have two threads Main thread and “thread” thread. So either “GFG” can be printed or “Geeks”, depend on which thread, thread scheduler schedule.For (a), the parent thread after calling start() method is paused and the thread scheduler schedules the child thread which then completes its execution. Following this, the parent thread is scheduled. For (b), the parent thread calls start() method but continues its execution and prints on the console. When join() method is called, the parent thread has to wait for its child to complete its execution. Thread scheduler schedules child thread while the parent waits for the child to complete.
2) What is the output of the following program?
public class Test implements Runnable{    public void run()    {        System.out.printf("GFG ");    }    public static void main(String[] args) throws InterruptedException    {        Thread thread1 = new Thread(new Test());        thread1.start();        thread1.start();        System.out.println(thread1.getState());    }}
a) GFG GFG TERMINATEDb) GFG TERMINATEDc) Compilation Errord) Runtime Error
Ans. (d)Explanation: Invoking start() method on a thread moves the thread to a RUNNABLE state. But invoking start() method on a thread that has already started throws a IllegalThreadStateException because the thread is already in RUNNABLE state.
3) What is the output of the following program?
public class Test extends Thread implements Runnable{    public void run()    {        System.out.printf("GFG ");    }    public static void main(String[] args) throws InterruptedException    {        Test obj = new Test();        obj.run();        obj.start();    }}
a) Runtime errorb) Compilation errorc) GFG GFGd) None of the aboveAns. (c)Explanation: Test class extends Thread class that has start() method implemented. So invoking start() on an object that extends Thread class invokes run() method defined in the program.
4) What is the output of the following program?
class myThread implements Runnable{    public void run()    {        Test.obj.notify();    }}  public class Test implements Runnable{    public static Test obj;    private int data;    public Test()    {        data = 10;    }    public void run()    {        obj = new Test();        obj.wait();        obj.data += 20;                  System.out.println(obj.data);    }    public static void main(String[] args) throws InterruptedException    {        Thread thread1 = new Thread(new Test());        Thread thread2 = new Thread(new myThread());                  thread1.start();        thread2.start();              System.out.printf(" GFG - ");       }}
a) 30 GFG –b) GFG – 30c) GFG –d) Compilation error
Ans. (d)Explanation: An object must first acquire a lock before calling wait() method. Also wait() method throws Checked exception(InterruptedException), we must include it in a try-catch block or throws it.
5)What is the output of the following program?
import java.util.concurrent.*;  public class Test implements Runnable{    public static CyclicBarrier barrier = new CyclicBarrier(3);    public void run()    {        System.out.printf(" GFG ");        try         {            barrier.await();        } catch (InterruptedException | BrokenBarrierException e)         {            e.printStackTrace();        }    }    public static void main(String[] args) throws InterruptedException    {        Thread thread1 = new Thread(new Test());        Thread thread2 = new Thread(new Test());                  thread1.start();        thread2.start();        System.out.printf(" GeeksforGeeks ");        try         {            barrier.await();        } catch (InterruptedException | BrokenBarrierException e)         {            e.printStackTrace();        }        System.out.printf(" End ");              }}
a) GeeksforGeeks GFG GFG Endb) GFG GeeksforGeeks GFG Endc) GFG GFG GeeksforGeeks Endd) All the above
Ans. (d)Explanation: For (a), the parent thread keep executing until it reaches the barrier. The child thread are then scheduled. For (b), thread scheduler scheduler thread1. Once it reaches the barrier, the parent thread is scheduled. Once parent thread reached the barrier, thread2 is scheduled. For (c), Both the child thread are scheduled. Finally when each of the child threads reach the barrier, the parent thread is scheduled.
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java-Multithreading
Java-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrow operator -> in C/C++ with Examples
Output of Java Program | Set 1
Output of C Programs | Set 1
delete keyword in C++
Output of C++ programs | Set 50
Runtime Errors
C++ Programming Multiple Choice Questions
Different ways to copy a string in C/C++
Output of C Programs | Set 2
Output of C++ Program | Set 1