• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 534
  • Last Modified:

Java Singleton

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
JianJunShen
Asked:
JianJunShen
  • 2
1 Solution
 
Gurvinder Pal SinghCommented:
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
 
objectsCommented:
> Is Synchronized key word needed for this method?

your queue is already thread safe, so no
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now