I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Originally posted by Barkat Mardhani:
I think only possible output is:
12
Reasons:
1. There are two threads.
2. At most one thread can access one of the synchronized methods.
3. One of the thread will get to synchronized method first. The other will wait.
4. The first method will increase the variable and print it. That will be 1. remember other thread has to wait.
5. After first thread is done with synchronized method, second thread will enter that method. Increase its value to 2 and print it.
6. The run methods of both thread will return.
7. The end.
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
That was my first impression. But look at closely at the code.
Originally posted by Barkat Mardhani:
I think only possible output is:
12
Reasons:
1. There are two threads.
2. At most one thread can access one of the synchronized methods.
3. One of the thread will get to synchronized method first. The other will wait.
4. The first method will increase the variable and print it. That will be 1. remember other thread has to wait.
5. After first thread is done with synchronized method, second thread will enter that method. Increase its value to 2 and print it.
6. The run methods of both thread will return.
7. The end.
synchronized static void m() ...
If the method m() would have been static, then I think the answer (b)12, wud have been the only correct answer.
Originally posted by Gopal Shah:
synchronized static void m() ...
If the method m() would have been static, then I think the answer (b)12, wud have been the only correct answer.
Originally posted by Barkat Mardhani:
I do not think so. Still there two separate objects. Two separate locks.
Originally posted by Andres Gonzalez:
I'm starting to get the feeling that all answers are possible.
stay tuned.
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Originally posted by Marlene Miller:
In my opinion System.out.println(x) is not atomic. Here are the byte codes. The first instruction loads the reference to the PrintStream object from memory.
..one rule states that all operations on primitive types, except in some case long and doubles, are atomic.
quote:
--------------------------------------------------------------------------------
Originally posted by Barkat Mardhani:
I do not think so. Still there two separate objects. Two separate locks.
--------------------------------------------------------------------------------
But there is only one class. The lock is now on the class level(class locks and object locks are two different thing).
If you run this code, you will notice that there is only one output at the beginning. This is because the other thread is waiting for the 1st thread to complete.
code:
--------------------------------------------------------------------------------
class t15 extends Thread { public void run() { aStaticMethod(); } public synchronized static void aStaticMethod() { System.out.println(currentThread().getName()); try { sleep(10000); } catch (InterruptedException e) {} } public static void main(String [] args) { new t15().start(); new t15().start(); }}
Does the word operation pertains to 1 opcode? Or does it mean one statement(which can be made up of a number of opcodes)?
SCJP2. Please Indent your code using UBB Code
SCJP2. Please Indent your code using UBB Code
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|