• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

thread on different instances?

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from K&B book, chapter 9, Self test Question 1.



Output:
Wallace-1 Gromit-1 Wallace-2 Gromit-2
OR
Gromit-1 Wallace-1 Gromit-2 Wallace-2



I'm expecting the output:

"Wallace-1 Wallace-2 Gromit-1 Gromit-2"
OR
"Gromit-1 Gromit-2 Wallace-1 Wallace-2 "

because there are 2 threads, and each has its "own" messanger instance, and won't block each other. please correct/explain me. thanks
[ November 27, 2006: Message edited by: Anthony Karta ]
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right about there being 2 messenger instances and them not blocking each other.

It is possible, that the Wallace thread gets interrupted (not blocked by the other thread) in its execution by the scheduler. Hence a Wallace or a Gromit thread is not guaranteed to complete execution before the other thread begins.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These two threads have the same default priority. So before thread 1 completes (output "Wallace-2"), thread 2 could start and then thread 1 finish, thread 2 finish. It is also possible that thread 2 completes before thread 1.
 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys... I get it now.



my next question is

do we need synchronized on message() method?? since they are running on different instances. I know there is no harmless but it's not necessary, am I correct??

I notice there is no different in the output.
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With 2 different instances, each would have a separate copy of the instance method. Hence, these 2 instances would not block each other.

"synchronized" would only matter if the same object were being accessed by multiple threads.
 
reply
    Bookmark Topic Watch Topic
  • New Topic