2

How can I write singleton to work through several processes? Is it possible? For example I have code that works with Remote Service in Android. How can I write singleton for this purpose?

user1074896
  • 1,025
  • 2
  • 15
  • 27
  • Not getting your question properly. – Lucifer Feb 07 '12 at 12:38
  • Is it possible to write Singleton class for several processes in Android? – user1074896 Feb 07 '12 at 12:39
  • http://groups.google.com/group/android-developers/browse_thread/thread/abe59add03ae4ce3 But I need to have only one singleton.Is it possible? – user1074896 Feb 07 '12 at 12:41
  • I have a Singleton class. In my app I am calling several methods of it to store some global information and then in my app from Remote Service(in separate process) I am calling several methods to get earlier stored information but I am getting new instance of Singleton with empty data fields. – user1074896 Feb 07 '12 at 12:48
  • What do you mean by process? Building a singleton is fairly simple - Gangnus' response shows one way of doing it - not necessarily the most common approach though. The singleton will be scoped by its classloader generally, so if by "process" you effectively mean JVM instance, then implementing it is significantly harder. – DaveH Feb 07 '12 at 12:58
  • Is your 'remote service' packaged into a separate apk? – NickT Feb 07 '12 at 13:04
  • No. I am using remote service to work with web-service. – user1074896 Feb 07 '12 at 13:09

3 Answers3

5

This thread is old, but the current accepted answer is wrong and misleading some people, so here we go.

Services in Android may run on the same process as your app, or a different process altogether.

If this is an Service defined by your own app for its internal use, it is probably running on the same process. Just don't set any of the process attributes on the Manifest. In that case, your service will run its tasks on either the main thread, or some background threads, and will share the same singleton instance with the rest of your app.

If this is a true remote Service running on a separate process, or a separate app, then what you are trying to achieve is much, much more difficult. Each process will have its own instance of the singleton, and they are in no way related to each other. This makes perfect sense, once you realise the different processes may not even be running the same version of the code.

If you really want to have a common object across processes (hint: you almost certainly don't), you'll need to create a shared memory space for its data, and implement some means of synchronisation. I'm sure there are full college lectures on that subject.

xaethos
  • 488
  • 6
  • 6
0

Remote Service you Create will always be singleton. You need to have an Interface to communicate between process using Android Interface Definition Language.

Android Interface Definition Language

See this Example http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.html

Vivek Khandelwal
  • 7,829
  • 3
  • 25
  • 40
-3
public enum MySingleton {
   SOLE;
   //all class stuff
}

This one is singleton that is simply written, lazy and thread safe. You can use MySingleton.SOLE directly or put it to other variables, but they all will be only references to one only instance. You can use it in as many processes as you wish, it shall weather them all. (processes in Android are threads really)

It is not my idea, it is from Effective Java, Second Edition

As for global data using (put that requirement into the question, please), you could reach MySingleton.SOLE from everywhere. Static constants are global. Of course, names would be yours.

Of course any thing has its workaround. You can fool this singleton by classloading.

If you really want to have one instance for several different tasks, not threads, then the easy solution will be singleton build as Hybernate or other ORM object. All processes will see the same row in the same table with the same data. But I think it is the overkill for your task.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • 2
    I need a class to make Singleton that I can use in several processes. But when I call getInstance() method of my singleton I am getting new instance of my singleton – user1074896 Feb 07 '12 at 12:45
  • Please, read my answer attentively. It is not class, it is enum! It is ABSOLUTELY impossible to make a second instance of SOLE. – Gangnus Feb 07 '12 at 12:46
  • I need a class to be Singleton not enum. How can I store some global data in enum?Thanks in advance. – user1074896 Feb 07 '12 at 12:49
  • Singleton is a *pattern*. It could be realized as a class (not safe), or as a set of arrays and structures (absolutely unsafe) or as enum (safe). Read books. The reference to a very nice one is in my answer. – Gangnus Feb 07 '12 at 12:52
  • It's OK. A month ago I was sure the safe singleton doesn't exist. – Gangnus Feb 07 '12 at 12:56
  • 6
    This answer is incorrect. A singleton created in this way will have multiple instances across processes. – howettl Oct 15 '15 at 02:39
  • @howettl You are mistaken. enum is lazy and thread-safe by itself. BTW, I have brought in the proof for my answer - by reference to the book that contains that proof. And if you do not agree, please, give some arguments. – Gangnus Oct 15 '15 at 11:26
  • Your book reference shows an elegant way to define a singleton using an enum but does not prove nor even mention working across process boundaries. See http://stackoverflow.com/a/2810386/758458 – howettl Oct 15 '15 at 15:47
  • @howettl Yes, it were you who mentioned them the first. So, the argumentation is on you. And AFAIK, services in Android work not as processes, but as threads. They all run on the single java machine. What is the use of the problem for the mentioned question? – Gangnus Oct 15 '15 at 18:25
  • You are confusing threads with processes. They are not the same thing. The question was about processes. – howettl Oct 15 '15 at 18:31
  • @howettl Not me is confusing, but the asker. HE thinks they are processes. I think they are threads. BTW, at your reference the author has mine point of view - different processes on different VM only. – Gangnus Oct 15 '15 at 19:07
  • @howettl I had added the solution for different processes, but I still think it is the overkill for the question mentioned. – Gangnus Oct 16 '15 at 08:38
  • 3
    @howettl is right. Processes in Android are _not_ threads. They are normal UNIX processes. "Services in Android work not as processes, but as threads" is also not true. A basic service on your own app will run on the *main* thread. IntentServices in Android help you run tasks on background threads. Services may also run on a separate process if you tell them to. Services will almost certainly run on a separate process if they belong to a different app. – xaethos Feb 10 '16 at 04:41