• 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

synchronization and _final_ modifier

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi every body.
I want to write a thread some think like this.

at line 4 the function is not syncronized I donot want to make this syncronized because I need performance. should I make this method final? and its parameter final?
Will it be thread safe if I do so?
Thanks
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think using the "final" modifier here will make any difference. Whether it is threadsafe or not depends on what hides behind those three little dots...
- Peter
 
koray guclu
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
those dots are not important when I run this code
thread 1 can call an instance of SimpleDateFormat(str) with a string str and another thread will run try to change this varianle str and also pass this value to that simple data format instance.
I wonder what will happen if I declare it final
because fialmodifier is used for constants.
If I use final I think that this will not be the same for every
Thread object and compiler will not use the same memory
because it is final.
What do you think?
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by koray guclu:
[...] and another thread will run try to change this varianle str [...]

It can't. Strings are immutable.
You may misunderstand the meaning of "final". When I say "final FooBar foobar", I'm creating a reference called "foobar" to a FooBar object. The reference is final and cannot be changed to point to a different FooBar object. The object it refers to, however, can still be changed unless it is of an immutable class such as String.
When you "change a String", in reality you are creating a wholly new String object and assigning a reference to the variable. In your code, myfunction() would still keep on using its reference to the old String so there's no problem at all. You can omit the "final" modifier, it has no impact on the issue.
If you have a mutable object and you want to prevent everyone else from changing it, "final" won't help you either. You'll have to create your own copy of the object. One of the most efficient ways to do this is to make your object Cloneable and implement clone().
- Peter
[This message has been edited by Peter den Haan (edited November 08, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic