The Artima Developer Community
Sponsored Link

Java Answers Forum
Singleton classes and Threadsafe

9 replies on 1 page. Most recent reply: Oct 22, 2004 11:48 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
Ganesh Kumar.V

Posts: 6
Nickname: ganeshv
Registered: Oct, 2004

Singleton classes and Threadsafe Posted: Oct 13, 2004 9:10 PM
Reply to this message Reply
Advertisement
Can any one tell me, how to make one class singleton. and please also tell me about threadsafe on singleton classes


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Singleton classes and Threadsafe Posted: Oct 18, 2004 10:16 AM
Reply to this message Reply
Singleton is a keyword in Java. Singleton classes
are declared static on the most part (makes sense
if there is only going to be one instance of it).
I've never used one with threads before but if you
run a google on them you are sure to find tons of
examples.

Ganesh Kumar.V

Posts: 6
Nickname: ganeshv
Registered: Oct, 2004

Re: Singleton classes and Threadsafe Posted: Oct 18, 2004 9:32 PM
Reply to this message Reply
thank you yaar

Nick Dwyer

Posts: 4
Nickname: ndwyer
Registered: Oct, 2004

Re: Singleton classes and Threadsafe Posted: Oct 19, 2004 1:57 AM
Reply to this message Reply
Gents, I don't know which version of Java that you are referring to - but my version doesn't have the 'singleton' keyword.

You really should have a look at the language specification
at:

http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#229308

There are plenty of other resources on java.sun.com that would be a good place to start. I would recommend that you start a course of study towards Java Programmer certification which would help take some of the guesswork out of your quest.

Also pay careful attention as to how to use threads correctly, issues relating to monitor states, locking etc.

I recommend "Taming Java Threads" by Allen Holub.

- good luck

Nick

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Singleton classes and Threadsafe Posted: Oct 19, 2004 2:05 PM
Reply to this message Reply
Several people know what this is about, and have collected their combined wisdom into a new and powerful tool called "Google". I have "power user" rights on this "Google" thing, and will let you click on this link for a modest fee:
http://www.google.com/search?q=java+thread+safe+singleton

Nick Dwyer

Posts: 4
Nickname: ndwyer
Registered: Oct, 2004

Re: Singleton classes and Threadsafe Posted: Oct 20, 2004 7:02 AM
Reply to this message Reply
Matt, shhh - you'll spoil the entertainment.

It's very kind of you to make this highly powerful tool available for general public use.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Singleton classes and Threadsafe Posted: Oct 20, 2004 10:59 PM
Reply to this message Reply
My bad on the use of language there... I don't think
I need to take a Java Certification course guy, I
encountered Singleton patterns in my software
engineering Soccer2003 project... And the fact
that one can have declarations such as

public Singleton instance() in Java led me to
believe its a keyword...

Once again my baad on the screw up of the terminology...
No need for the sarcasm and the shrewed remarks...
I led the guy in the right direction...
If he knows how to use it in a Java context, who
really cares whether or not its a key word?
If he ends up reading up on Design Patterns it
would probably help him out more than knowing it
in the Java context but I thought I'd try and
dispell it from the Java point of view...
My baad on the misquote there...

One,
Spike

> Gents, I don't know which version of Java that you are
> referring to - but my version doesn't have the 'singleton'
> keyword.
>
> You really should have a look at the language
> specification
> at:
>
> http://java.sun.com/docs/books/jls/second_edition/html/lexi
> cal.doc.html#229308
>
> There are plenty of other resources on java.sun.com that
> would be a good place to start. I would recommend that you
> start a course of study towards Java Programmer
> certification which would help take some of the guesswork
> out of your quest.
>
> Also pay careful attention as to how to use threads
> correctly, issues relating to monitor states, locking
> etc.
>
> I recommend "Taming Java Threads" by Allen Holub.
>
> - good luck
>
> Nick

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Singleton classes and Threadsafe Posted: Oct 20, 2004 11:20 PM
Reply to this message Reply
My baad again... To the poster...
Apparently its a return type...
I guess it could be researched on the Java API...

> My bad on the use of language there... I don't think
> I need to take a Java Certification course guy, I
> encountered Singleton patterns in my software
> engineering Soccer2003 project... And the fact
> that one can have declarations such as
>
> public Singleton instance() in Java led me to
> believe its a keyword...
>
> Once again my baad on the screw up of the terminology...
> No need for the sarcasm and the shrewed remarks...
> I led the guy in the right direction...
> If he knows how to use it in a Java context, who
> really cares whether or not its a key word?
> If he ends up reading up on Design Patterns it
> would probably help him out more than knowing it
> in the Java context but I thought I'd try and
> dispell it from the Java point of view...
> My baad on the misquote there...
>
> One,
> Spike
>
> > Gents, I don't know which version of Java that you are
> > referring to - but my version doesn't have the
> 'singleton'
> > keyword.
> >
> > You really should have a look at the language
> > specification
> > at:
> >
> >
> http://java.sun.com/docs/books/jls/second_edition/html/lexi
>
> > cal.doc.html#229308
> >
> > There are plenty of other resources on java.sun.com
> that
> > would be a good place to start. I would recommend that
> you
> > start a course of study towards Java Programmer
> > certification which would help take some of the
> guesswork
> > out of your quest.
> >
> > Also pay careful attention as to how to use threads
> > correctly, issues relating to monitor states, locking
> > etc.
> >
> > I recommend "Taming Java Threads" by Allen Holub.
> >
> > - good luck
> >
> > Nick

PAsh

Posts: 2
Nickname: pashar123
Registered: Oct, 2004

Re: Singleton classes and Threadsafe Posted: Oct 22, 2004 9:06 AM
Reply to this message Reply
Singleton classes have their constructor PRIVATE.
AT any point of time there will be only one instance of that class.
To make it threadsafe:
You can make the code where object is initialized as synchronized.Please see the code below.

package com.jgk.patterns.singleton;

public class JGKSingleton {

/* Here is the instance of the Singleton */
private static JGKSingleton instance_;

/* Need the following object to synchronize */
/* a block */
private static Object syncObject_;

/* Prevent direct access to the constructor
private JGKSingleton() {
super();
}


public static JGKSingleton getInstance() {

/* in a non-thread-safe version of a Singleton */
/* the following line could be executed, and the */
/* thread could be immediately swapped out */
if (instance_ == null) {

synchronized(syncObject_) {

if (instance_ == null) {
instance_ = new JGKSingleton();
}

}

}
return instance_;
}
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Singleton classes and Threadsafe Posted: Oct 22, 2004 11:48 AM
Reply to this message Reply
Um, didn't you just copy and paste that from the jGuru article (http://www.jguru.com/faq/view.jsp?EID=124425), item #4 in the Google list (http://www.google.com/search?q=java+thread+safe+singleton) posted above?

You might want to look a just little more closely at the comments, by the way...

Flat View: This topic has 9 replies on 1 page
Topic: communication modulations demos Previous Topic   Next Topic Topic: java telephone interface

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use