• 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

String as a lock

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,all:
Given:

Primitives don't have locks and therefore can not be used to synchronize threads. Here String s is used for synchronize as a lock.


String instance itself is shared between the two threads and therefore the lock on the String is also shared between the two threads. The shared lock on the shared String literal will provide effective synchronization.


What is the share relationship here?
Thanks,
Roger
[ September 04, 2003: Message edited by: Roger Zhao ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String literals like "" are automatically shared, even across classes; i.e., all occurrences of the literal "" refer to the same object. The code

will print "true", whereas

because it creates two separate String objects, prints "false".
 
reply
    Bookmark Topic Watch Topic
  • New Topic