• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 545
  • 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

  • 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.

> Is Synchronized key word needed for this method?

your queue is already thread safe, so no
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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