Shall a plug-in be prepared to handle several instances of itself running, or it may assume to be a singleton?
Singleton?
-
- English
- Miwarre
- Thread is marked as Resolved.
-
-
The plugin can spawn as many threads to handle whatever synch or asynch processes you need to do. Not sure you would need multiple copies of a plugin. Java is object oriented so the plugin should be able to create any number of instances of any class bundled in the jar.
Object1 = new object("foo");
Object2 = new object ("bar); -
Hmm.. what do you mean exactly with "handle several instances of itself running"? The server only loads the plugin once, but of course you can create as many threads as you want (as @zfoxfire mentioned), or use an executor / scheduler, or the "out-of-the-box" Timer class (although it does not provide any kind of parallelism). You can also define your events to be called in a separate thread (by defining the threading type in the @EventMethod annotation).
But something tells me you mean something else? -
Hmm.. what do you mean exactly with "handle several instances of itself running"? [...]
But something tells me you mean something else?Uh, yes... I meant instances of the plug-in 'main' class, the one derived from net.risingworld.api.Plugin. Of course, internally, the plug-in may instantiate any number of ancillary or utility classes it may define, starts threads, etc, it may even spawn new instances of itself (if it likes a dangerous life...) but, being the originator, it will be responsible for the correct management of all these details, as always is the case.
OTOH, whether the main class can have multiple instances or not, outside of the knowledge or the initiative of the plug-in itself, influences for instance the strategies for defining fields or methods as static or not, for db management and in general for how paranoid about concurrency one should be.
If a server starts only one instance of each plug-in and as there is only one server in each RW context, I cannot see a use case for this myself, but I prefer to be sure, as I may easily overlook something.
-
Not sure if I know exactly what you mean... there is only one single main class (subclass of net.risingworld.api.Plugin). It doesn't matter if you create several other classes in your plugin which are also derived from net.risingworld.api.Plugin for example, only the "main class" (which needs to be defined somewhere) will be instantiated by the server. Even if you create new instances of your main class somewhere, it's up to you what you do with them (the server doesn't care about them).
Is there something specific you want to achieve, or is this just out of curiosity?
Participate now!
Don’t have an account yet? Create a new account now and be part of our community!