Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1061731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:31:00+00:00 2026-05-16T18:31:00+00:00

I am implementing an service class which uses the services of the third party

  • 0
  1. I am implementing an service class which uses the services of the third party providers.
  2. User will interact with the Service class to get the service of the service provider.

My requirement:
I want to load the service class only once( or it persist for long time in memory say 2 Hr.).

My question:
How can I check that this service class is loaded only once?
If it is loaded each time an request is made so is there any way to them persist in the memory?

Here is the code of my Service class

package com.example;
import com.example.spi.Multiplication;
import java.util.Iterator;

import java.util.ServiceLoader;

public class MultiplicationService implements Multiplication{

    private static MultiplicationService service;
    private ServiceLoader loader;
    public static int no=0;
    private MultiplicationService() {       
        loader = ServiceLoader.load(Multiplication.class);
   }

     public static synchronized MultiplicationService getInstance() {
        if (service == null) {
            service = new MultiplicationService();
        }         
        return service;
    }

     public int getMultiplication(int a,int b){
        int result = 0;

        try {
            Iterator multiply = loader.iterator();
        while (result == 0 && multiply.hasNext())
            {  
                Multiplication d = multiply.next();
                result = d.getMultiplication(a,b);
                System.out.println("Result is : "+ result);
            }
        } catch (Exception serviceError) {
            result = 0;
            serviceError.printStackTrace();

        }
        return result;
    }
 }

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:31:01+00:00Added an answer on May 16, 2026 at 6:31 pm

    You can implement the singleton pattern. One option is using an enum:

    public enum Service {
        INSTANCE;
    
        private ThirdPartyService service = new ThirdPartyService();
    
        public ThirdPartyService getService() {
            return service;
        }
    }
    

    But don’t overuse that pattern, because it makes it hard to unit-test your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What all language components will have to be used for implementing an email service
we are implementing a new web service. The web service will be a store
I am implementing an application with get the response from service and show to
I've been implementing a GWT application that calls a REST-service (which we're also developing).
I am working on a Widget which will get the Current GPS location and
I am implementing a billing service based on the Dungeons sample (which is the
I'm currently implementing a service that uses WCF discovery and provides Discovery Endpoint and
I have a very simple WCF4 Restful web service which uses WcfRestContrib to allow
I'm implementing service binding into my application. However when i start my activity which
Being completely new to WCF, I'm trying to make a RESTful service which will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.