Skip navigation links

Package net.pterodactylus.util.service

Basic interface and implementation for services that can be started, stopped and run asynchronously in the background.

See: Description

Package net.pterodactylus.util.service Description

Basic interface and implementation for services that can be started, stopped and run asynchronously in the background.

Usage of the Service Interface

Using a Service is pretty straight-forward:

  Service someService = new SomeService();
  someService.init();
  someService.setParameter1(...);
  someService.setParameter2(...);
  // some more preparations
  // and finally...
  someService.start();
 

Implementing Own Services

If you want to implement your own service it is recommended that you override the AbstractService base class as it takes care of a couple of things for you, like creating the thread that runs your service, managing the Service.stop() method, providing synchronized access to state variables, and other things.

The AbstractService base class adds a service method for each of the Service.init(), Service.start(), Service.stop(), and Service.destroy() methods that let you take care of things in your own service without having to interfere with the stuff at AbstractService does for you. The AbstractService.serviceRun() method is the place to implement your magic then. It should be designed as an infinite loop like this:

 protected void serviceRun() {
        while (!shouldStop()) {
                doStuff();
                doLengthyStuff();
                if (shouldStop()) {
                        continue;
                }
                doMoreLengthyStuff();
        }
 }
 
Checking the AbstractService.shouldStop() every once in a while ensures that your service stops within a short time after Service.stop() has been called.
Skip navigation links

Copyright © 2026. All rights reserved.