store data in Singleton classes
I have an application in java that uses two different threads.And for sharing data between these two threads I wanna use a Singleton class.
The data that needs to be shared between threads is (Latitude,Longitude).
And here is my problem:What should I use for storing this data in the Singleton class?
Some Lists..?
And if you can also provide an example it would be fantastic.Thank you!
Is this Singleton ????How cou开发者_如何学运维ld I make it non-Singleton
EDIT:
I have a java app that does the following thing:
1.The first thread(which is a ThreadPool)-which I believe will also write in the BlockingQ Listens to one port for incoming connections from five different users Let's call them :
user1 user2 user3 user4 user5
each of them sending GPS data.
2.The second thread-which will read from the BlockingQ. In the same time my java app listens to a second port where waits for another client(different from those who send GPS data) to connect to it.
Now...I have a second app that connects to the java app that I've just described it.
In this second app I have a list user1...user5 and depinding on which item I will choose(user1...5) I have to receive the correct data from there.
So now....how do I write/read the data in the BlockingQ in order for me to receive the correct data???
I think what you are trying to achieve is a Producer Consumer Pattern. You should use a BlockingQueue
and an immutable Coordinate
object with Latitude and Longitude for that.
Coordinate
should only have the two fields Longitude and Latitude that are both final
fields and can only be set with the constructor. This way you can make sure, that you don't have any race conditions or change the fields by accident with the wrong thread.
BlockingQueue
does not store the data permanently, so only if you need to do that you could create a Singleton.
The better solution would be to create a class like ShareData and just pass them to the two threads so you can save yourself some time creating a threadsafe Singleton. This could look like this:
public interface ShareData {
public void AddToList(Coordinate coord); // add to BlockingQueue and an internal store here
public Coordinate TakeFromList(); // take from BlockingQueue
}
The Coordinate
class should look like this and is threadsafe, since it is immutable and cannot change its value.
public class Coordinate {
private final double lon;
private final double lat;
public Coordinate(double lon, double lat) {
this.lon = lon;
this.lat = lat;
}
public double getLon() {
return lon;
}
public double getLat() {
return lat;
}
}
My advice would be to not bother with the singleton. Singletons are almost never a good idea. If you don't want more than one of them, just don't make more than one of them. If you want it global, declare your one object of the class global (but better yet, just pick a class to own it.).
I don't know your program, but I'd lay pretty good odds that you will never see a bug caused by somebody inadvertantly making more than one of your shared data class objects. However, getting thread-safe singletons coded right is a very sticky problem, and you are very likely to have bugs if you try to do that.
And then what happens one day when you discover that you need another pair of those communicating threads for some reason? You'd want another one of those shared data objects to go with them of course, but because you made it a singleton it becomes a major rewrite of that class rather than a triviality.
Just make a class to store that information. lat/lon can be represented by doubles.
public class OuterClass {
public static synchronized InnerSingleton getSingleton() {
if(singleton == null) {
singleton = new InnerSingleton();
}
return singleton;
}
private InnerSingleton singleton;
class InnerSingleton {
private double lat, lon;
private InnerSingleton() { }
public double getLatitude() { return lat; }
public double getLongitude() { return lon; }
public void setLatitude(double l) { lat = l; } //maybe validate? -90 <= lat <= 90
public void setLongitude(double l) { lon = l; } //maybe validate? -180 <= lon <= 180
}
}
I'd advise a concurrent queue
look in java.util.concurrent for several implementations that could help you: ConcurrentLinkedQueue, ConcurrentBlockingQueue
so you have a singleton queue that one thread reads from and the other writes to
精彩评论