473,378 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Whats wrong in the code snippet below ?

lifeisgreat20009
(This is not a trick question, there is a potential real problem in using this code)

Expand|Select|Wrap|Line Numbers
  1. class Singleton{
  2.          public static Singleton Instance() {
  3.                   if (_instance == null)
  4.                        _instance = new Singleton();
  5.                   return _instance;
  6.          }
  7.          protected Singleton() {}
  8.          private static Singleton _instance = null;
  9. }
Mar 30 '09 #1
7 1516
JosAH
11,448 Expert 8TB
Multiple Threads, each trying to obtain your 'singleton'. Synchronize that static method.

kind regards,

Jos
Mar 30 '09 #2
dmjpro
2,476 2GB
@lifeisgreat20009
Actually no syntactical error with your code. Then how did you say that it's error?
Mar 31 '09 #3
JosAH
11,448 Expert 8TB
@dmjpro
It helps if you read the entire thread first before you hit the 'Submit Reply' button.

kind regards,

Jos
Mar 31 '09 #4
dmjpro
2,476 2GB
@Joash
Sorry!! :(

If i write the code like this...
Expand|Select|Wrap|Line Numbers
  1. public SingleTone{
  2. private static _instance = new SingleTone();
  3. public static getInstance(){
  4. return _instance;
  5. }
  6. }
  7.  
Then should i synchronize it?
Apr 3 '09 #5
r035198x
13,262 8TB
Why not find an article about the Singleton and read it?
Apr 3 '09 #6
JosAH
11,448 Expert 8TB
@dmjpro
Think: a class is only loaded once by a classloader.

kind regards,

Jos

ps. btw it's 'Singleton', not 'SingleTone'.
Apr 3 '09 #7
further to Jos' idea of synchronising the static method, the constructor should be private so that noone instantiates it except itself.

http://java.sun.com/developer/techni...ng/singletons/
Apr 6 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Vic | last post by:
Dear All, I found this code snippet on this list (taken from a nice webpage of a courteous fellow), which I used to filter a form on a combo box. I wanted to repeat the same code to have an...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
7
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell...
5
by: hiqu | last post by:
This issue is driving me nuts and not able to figure out whats wrong. I've this code in my firefox extension. Firefox always hangs and reports the script is busy. if I introduce a break...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.