Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads question-15 needs justification

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i think this question has been discussed earlier, sorry but i can't able to find that thread.

two fragments
I synchronized void chat(long id) {
II void chat(long id) (
Output:
with II
yo yo dude dude
with I
yo dude yo dude

options:
A an exception is thrown at runtime.
B with I,compilation fails.
C with II,compilation fails.
D with I,yo dude dude yo.
E with I,dude dude yo yo.
F with II,yo dude dude yo.
ANSWER is F .
yo dude dude yo. how?

also please tell me at line 14, d instance variable is static,so how can we able to access it from non-static method go()?

and if i remove static from line 14,it is giving me a java.lang.NullPointerException.
explain this please.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid posting in all uppercase. Thanks.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question discussed recently here, have a look here
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And where is your main() method?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:And where is your main() method?


look below carefully in code.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arjun srivastava wrote:
look below carefully in code.

Oh, Sorry, I don't notice. Thanks!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, let's clear your basic doubt.
arjun srivastava wrote:
also please tell me at line 14, d instance variable is static,so how can we able to access it from non-static method go()?

Static variable are loaded at class time, so you can access from instance methods. But, other way is wrong. You need a object to access a instance method from static context. I think, you confused with these two!
arjun srivastava wrote:
and if i remove static from line 14,it is giving me a java.lang.NullPointerException.
explain this please.


OK, if you declare Dudes as a instance variable of DudesChat class, then, what about the DudesChat object, which you've created in the line18 and 19? Do they have not null Dudes object?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got it.
thanks!
and please also tell me about the output part,i saw the previous thread ,but didn't get through.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arjun srivastava wrote:
and please also tell me about the output part,i saw the previous thread ,but didn't get through.

OK, let's fragment this code..


Here, you're passing Thread ID to your chat() method of Dudes object. And in the Dudes object, it's flag variable is initialized to 0. So, in the if condition, you reset it with your thread's ID. For the first thread, after set the flag variable to thread ID, both are same, so yo yo will be printed. And for the second thread, the Dudes's flag variable is set previous thread ID, so dude dude will be printed. And the chat() method is synchronized, one thread will invoke the method on Dudes object.

Hope this makes sense! For the other fragment, you can get the logic easily.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:
class Dudes {
static long flag=0;
synchronized void chat(long id) {
if(flag==0)
flag=id;
for(int i=1;i<3;i++) {
if(flag==id)
System.out.println("yo");
else
System.out.println("dude");
}
}
}
public class DudesChat implements Runnable {
static Dudes d;
void go() {
d=new Dudes();
new Thread(new DudesChat()).start();
new Thread(new DudesChat()).start();
}
public void run() {
d.chat(Thread.currentThread().getId());
}
public static void main(String[] args) {
new DudesChat().go();
}
}
[/code]
Here, you're passing Thread ID to your chat() method of Dudes object. And in the Dudes object, it's flag variable is initialized to 0. So, in the if condition, you reset it with your thread's ID. For the first thread, after set the flag variable to thread ID, both are same, so yo yo will be printed. And for the second thread, the Dudes's flag variable is set previous thread ID, so dude dude will be printed. And the chat() method is synchronized, one thread will invoke the method on Dudes object.

thanks for reply
for fragment II,
which is not synchornized,any thread out of two can invoke the chat() method.
so output can be unpredictable.
for fragment I,
chat() is synchronized,here only one thread can access the method at a time.
so output
has to :yo yo dude dude.
isn't it? correct me if i am wrong.
but in book, F is answer:with fragment II yo dude dude yo
tell me this logic behind it.
is it correct or what?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arjun srivastava wrote:
is it correct or what?


When you write code, please do proper fragments, otherwise, difficult to read. Try this code, you can get it.


That is one of the possible answers. And find the logic behind it.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:


i have compile your code at least infinite times for me,but with chat() as synchronized.
output :
yo - 8
yo - 8
dude - 9
dude - 9

but it never shows yo dude dude yo with chat() as synchronized
which is the answer given in book.
hope you are getting me well this time.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arjun srivastava wrote:i have compile your code at least infinite times for me,but with chat() as synchronized.
output :
yo - 8
yo - 8
dude - 9
dude - 9

but it never shows yo dude dude yo with chat() as synchronized
which is the answer given in book.
hope you are getting me well this time.


with thread we cannot determine the output for sure.
we can only expect that this should be the output..
but the complete behavior will be determine by the JVM..

even if you run the above code on different JVM you may find the difernt output on different JVMs
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arjun srivastava wrote:
but it never shows yo dude dude yo with chat() as synchronized
which is the answer given in book.
hope you are getting me well this time.


OK, let's discuses the logic behind this.

There is a competition between threads, which one first invoke the chat() method of the object. If one got the first chance to through the method, it will change the flag variable to its thread Id, and it'll print yo, and the other thread will print dude.

For the output, you've mentioned, this will be the situation.
Let name the threads as A and B. If the thread A got the chance to invoke the chat() method, it will change the flag variable, and the thread B can invoke the method, since it's not synchronized. But, the second thread, B can't change the value of the flag variable. Because, the flag variable isn't zero. And thread A will print yo first time, and the thread B will print dude and again, it can print the dude before the thread A print yo next time. This is the scenario behind this.

I think, you got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic