|
>If you'd like to play safe game you should
remove condition in line 23 or make the whole method
synchronized.
Condition in ine 23 can't be removed,
becose synchronization in next line is "bottler's cone" (week
place).
If we
remove it the code:
21: public
static Singleton getInstance() {
22: synchronized (synchronizer) { 23: if (instance == null) { 24: instance = new Singleton(...); 25: } 26: } 27: return instance; 28: } this
just equive to code:
21: public
static synchronized Singleton
getInstance() { 22: if (instance == null) { 23: instance = new Singleton(...); 24: } 25: return instance; 26: } But
this code mean that for any request only one thread may use this method (always
synchronized). Why? I only want to get object instance. So, i can't make method
syncrhonized, becose to read created instance it not nessary. But i must make
syncronize instance creation. Ok. Then i already have instance i'm do not
synrhonized read instance, otherwise i do synchronized instance creation. Where
I wrong?
>we
can't support multithread logic in analyzer because it would make any
inspections completely useless. Imagine we have:
>a = 5;
>if (a == 5) {}
Why?
Logic are very simple. The (a == 5) may
be false then:
1. variable a
is global variable of class
2. variable a is not
final
3. method with
it conditions alocated is not synchronized (or have syncronized block by local
static variable)
PS. I think too that
thread analis is very interect. Becose java used in Internet projects as
language leader. Many requested pages (JSP or servlets, singletons - it's child
of Internet life) may contains any troubles with synchronization or threads
collisions.
From: Maxim Shafirov [mailto:[EMAIL PROTECTED]] Sent: Thursday, March 21, 2002 6:57 PM To: [EMAIL PROTECTED] Subject: Re: [Eap-list] BUG: Code Inspection: Singleton not recognized
|
Title: Сообщение
- [Eap-list] BUG: Code Inspection: Singleton not recognized Alexey A. Efimov
- Re: [Eap-list] BUG: Code Inspection: Singleton not r... Maxim Shafirov
- RE: [Eap-list] BUG: Code Inspection: Singleton not r... Alexey A. Efimov
- RE: [Eap-list] BUG: Code Inspection: Singleton n... Mike Aizatsky
- [Eap-list] Full screen blinking Christopher Cobb
- [Eap-list] Re: Full screen blinking -- f... Christopher Cobb
- SV: [Eap-list] BUG: Code Inspection: Singleton not r... Patrik Andersson
- RE: [Eap-list] BUG: Code Inspection: Singleton not r... Alexey A. Efimov
- RE: [Eap-list] BUG: Code Inspection: Singleton not r... Alexey A. Efimov
- RE: [Eap-list] BUG: Code Inspection: Singleton n... Jonas Kvarnstr�m
- RE: [Eap-list] BUG: Code Inspection: Singleton not r... Tom Wagner
- RE: [Eap-list] BUG: Code Inspection: Singleton n... Jonas Kvarnstr�m
- RE: [Eap-list] BUG: Code Inspection: Singleton not r... Alexey A. Efimov
