• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Do separate threads have separate values for non-initialized final variable?

 
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is final variable thread safe? Do different threads have separate copy of final variable?

Thanks.
 
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is a “non-initialized final variable”? Isn't that a contradiction in terms?

Arun Singh Raaj wrote:Is final variable thread safe?

No. Not unless it is a primitive or immutable or created from a thread‑safe class.

Do different threads have separate copy of final variable?

Thanks.

Yes and no. Only local variables are created on the thread's stack, so there is no such thing as separate copies of fields.
 
Arun Singh Raaj
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What is a “non-initialized final variable”? Isn't that a contradiction in terms?


A blank final variable :

Yes and no. Only local variables are created on the thread's stack, so there is no such thing as separate copies of fields.


If multiple users means multiple threads are accessing this variable concurrently and don't have separate copies of this variable then how would they assign their own values to this variable?
Thanks.
 
Campbell Ritchie
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun Singh Raaj wrote: . . . A blank final variable : . . .

That isn't blank; that field is initialised before the constructor completes. This is a blank field:-The compiler won't like that and that class won't compile as it stands.

If multiple users . . . are accessing this variable concurrently . . . then how would they assign their own values to this variable?
Thanks.

You can't assign a final field twice; it will fail to compile.See, final variables, so final that you can even use the variable in a λ, and see it will reliably count to 200,000,000 showing you whether it is thread‑safe or not.

[critchie@??? java]$ java counter.BadCounterDemo
Counter: counted 0
Counter: counted 76667206
Counter: counted 100092580
[critchie@??? java]$ java counter.BadCounterDemo
Counter: counted 0
Counter: counted 79646710
Counter: counted 100619365
[critchie@??? java]$ java counter.BadCounterDemo
Counter: counted 0
Counter: counted 99210948
Counter: counted 101661418

I think it is in his book Core Java SE9 for the Impatient that Cay Horstmann points out that sort of race condition occurs much worse with modern multi‑core processors than it used to with single‑core processors. Yes, that is the right reference: page 345.
 
Arun Singh Raaj
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
omg, such a simple question you made it so ugly.

it compiles very well.

I was hoping answer regarding separate copies of final variables.
 
Saloon Keeper
Posts: 26290
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"final" simply means you can assign a value exactly once. Either when the variable is declared or in a constructor (but not both). It is then forever after immutable. Not quite like "const" in C, but close. You can also declare method-local variables as final, but they are destroyed when you exit the method or block you declared them in.

But final is only a modifier for variables. The actual variable may be immutable, but if it's a member variable, you have a separate copy of it for each instance of the class. If you declare it "static final", then there will be one and only one instance of that variable for all instances of the class (and for access in a static context). No difference between final static and ordinary static except for its mutability.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is, however, possible to "leak" the instance into other thread before final variable is assigned.

Be warned. This code is obscure.
It shows why you shouldn't "leak" the this from a constructor and why you shouldn't start threads inside a constructor.



It prints:
[The value is: null]
[The value is: value]

 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic