Menu
Topics Index
...
`

HaiBujji


If you need explanation Read this topic

If you need Answer Take test on this topic

HaiBujji
What will be the output of the following program?
public class HaiBujji implements Runnable {
    private int x;
    private int y;
    public static void main(String[] args) {
        HaiBujji that = new HaiBujji();
        (new Thread(that)).start();
        (new Thread(that)).start();
    }
    public synchronized void run() {
        for (int i = 0; i < 3; i++) {
            x++;
            y++;
            System.out.print(x + " " + y + ", ");
        }
    }    
}

A. 0 0, 1 1, 2 2,
B. 1 1, 2 2, 3 3, 1 1, 2 2, 3 3,
C. 0 0, 1 1, 2 2, 3 3, 4 4, 5 5,
D. 1 1, 2 2, 3 3, 4 4, 5 5, 6 6,
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 D..here we have the concept of the Synchronized keyword which is used for the Thread-safe methods, if we use the synchronized keyword than that object can't used by more than one thread at time..
Execution:
here we have the class HaiBujji which is implementing the Runnable interface and HaiBujji class has the 2 attributes x and y..in the main() we create the instance for the class so memory is allocated for those x and y..next we are creating the objects for the Thread class by  passing the object of the HaiBujji class and next we are calling the start() so run() get invoked and starts the execution of the thread..here we started to threads but 2 threads can't work at same time..here both are working on the same object so loop will repeat 6 and x and y values will increase ti 6

Posted by Uday Kumar    2015-02-27 03:45:25


ANS IS D (1 1, 2 2, 3 3,4 4,5 5,6 6)

--Here,the concept of thread is used.

--First of all, HaiBujji class is defined which implements the Runnable interface which mean in class we have to override the run() method.

--Run() method is automatically called when the thread is started i.e. when the thread is in ready Queue and whwnever processor gets time it takes the thread in execution.

--But here run() method is synchronized which means at a time onle one thread can execute.

--After that object of class HaiBujji is created and  2 threads are started using that object.

--In run method for loop runs from 0 to m in which x and y are incremented by 1.

For 1st Thread:
1 1
2 2
3 3
will et printed.

Now 2nd Thread:
4 4
5 5
6 6
will get printed.

As x and y are instance variables so values will be continued.

OUTPUT:
1 1 ,2 2,3 3,4 4,5 5,6 6

Posted by Mânïshå Mùlchåndânï    2015-02-27 07:19:06


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Asad Ahmed. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-02 06:35:28


© meritcampus 2019

All Rights Reserved.

Open In App