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
-----------------------------------------------------<BR> Koray GUCLU ~ the sky has no limit ~<BR> (B.s. Computer Engineer)<BR> Frankfurt am Main, Germany<BR> web : <A HREF="http://www.geocities.com/korayguclu/<BR rel="nofollow">" TARGET=_blank>http://www.geocities.com/korayguclu/<BR></A> mail: korayguclu@yahoo.com<BR>-----------------------------------------------------
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
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
Joined: Feb 23, 2001
Posts: 59
posted
0
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
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
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).]
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.