Solved

Java Singleton

Posted on 2011-02-23
3
506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
servlet and mdb, jms error 1 81
DTD and JAVA versions 1 55
JDBC performance 8 33
Java - accessing a parent class member when another class inherits from the parent class 9 56
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

732 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