• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tread synchronized  RSS feed

 
ping zhang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Result:
--------------------------

Sender put : 1
Sender put : 2
Receiver get : 1
Sender put : 3
Receiver get : 2
Receiver get : 3
Receiver get : 4
Sender put : 4
Receiver get : 5
Sender put : 5
Sender put : 6
Sender put : 7
Receiver get : 6
Sender put : 8
Receiver get : 7
Receiver get : 8
Receiver get : 9
Sender put : 9


--------------------------
Why?

There should be most two lines are the same (put or get), but there are three lines, why?

Thanks!

[ March 27, 2006: Message edited by: ping zhang ]
[ March 27, 2006: Message edited by: ping zhang ]
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why?

There should be most two lines are the same (put or get), but there are three lines, why?

Thanks!


It looks fine to me. No numbers are lost -- every put has a matching get.

Remember, the printing of the numbers are not synchronized. Furthermore, there may be some buffering involved....

BTW... I am not sure what you mean by "two lines" versus "three lines"?

Henry
[ March 28, 2006: Message edited by: Henry Wong ]
 
ping zhang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for you reply.

I know the printing of the numbers are not synchronized, but the printing statement including synchronized method "get()". When the "get()" is not invoked, the "put()" will be waiting, for the flag "isEmpty" is false.

And forget the "two lines" or "three lines", that's my misunderstanding, because I thought the printing of the numbers not synchronized will cause continued two line "get()" or "put()".


[ April 03, 2006: Message edited by: ping zhang ]
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the printing of the numbers are not synchronized, but the printing statement including synchronized method "get()". When the "get()" is not invoked, the "put()" will be waiting, for the flag "isEmpty" is false.


In the few cases, where the get() seems to be "getting ahead" of the put(), it is most likely caused by the printing system. Remember, writing to the "system out", writes to a printstream. It may take a bit of time before it gets to the screen.

Henry
 
ping zhang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh...........!

Thanks, thank you very much!
 
ping zhang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


In the few cases, where the get() seems to be "getting ahead" of the put(), it is most likely caused by the printing system. Remember, writing to the "system out", writes to a printstream. It may take a bit of time before it gets to the screen.

Henry


Oh, I think there is antoher question.

The strings of "put()/get()" from printstream to console may take a bit of time, but printstream should be in sequence, that is to say one "get()" must be after one "put()". That's not the output reuslt.
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ping zhang:


Oh, I think there is antoher question.

The strings of "put()/get()" from printstream to console may take a bit of time, but printstream should be in sequence, that is to say one "get()" must be after one "put()". That's not the output reuslt.


Okay, now we are in the "based on personal experience" territory. I don't have a clear cut answer here... So take this answer with a big grain of salt. Or feel free to ignore me...

Remember, besides being "buffered", this buffering is also done without synchronization. And based on personal experience, I noticed that it is possible for one thread to get ahead of another thread during output. And this gets more complex with more threads.

However, I also noticed that if synchronization is used prior to printing, it should not get more than one ahead -- as it will synchronize back with each iteration. This means that with get(), it should get no more than one ahead, and with put(), it should get no more than two ahead.

Of course, even with this, it could change. This is the realm of race conditions.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!