Solved

Java Singleton

Posted on 2011-02-23
3
504 Views
Last Modified: 2012-05-11
I have a singleton class. If I have a business logic method (Mulitple threads will invoke this method). Do I need to put it as synchronized method. Please see the codes below:  
public class Resource{
    public static Resource self = new Resource();
    Public static ConcurrentLinkedQueue<Item> queue = new CurrentLinkedQueue<Item>();
    
    public static Resource getInstance(){
          return Reource();
    }  
    
    private Resource(){
            
    } 

    public Item getNextItem(){                           <----------------------Is Synchronized key word needed for this method?
          return queue.poll(); 
    } 
}

Open in new window

0
Comment
Question by:JianJunShen
  • 2
3 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34967509
if <<return queue.poll(); >> is only fetching the value and not updating anything, then you dont need the Synchronized keyword.
Else, it is required and it will depend upon where you want the sync control to be. And i would suggest that you put the synchronize keyword in the CurrentLinkedQueue class itself if at all you want that resource to be shared.



0
 
LVL 92

Expert Comment

by:objects
ID: 34967529
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 34967537
> Is Synchronized key word needed for this method?

your queue is already thread safe, so no
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
Apps blocked by Java 9 88
login jsp example 24 67
jmss example java 2 23
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question