将数据存储在 Singleton 类中

发布于 2024-11-09 03:28:40 字数 651 浏览 0 评论 0 原文

我有一个 java 应用程序,它使用两个不同的线程。为了在这两个线程之间共享数据,我想使用 Singleton 类。

线程之间需要共享的数据是(纬度,经度)。

这是我的问题:我应该使用什么来将这些数据存储在 Singleton 类中?

一些清单..?

如果您也能提供一个例子那就太好了。谢谢!

这是单例吗????我怎样才能使它成为非单例

编辑:

我有一个java应用程序,它执行以下操作:

1.第一个线程(它是一个线程池)-我相信它也会写在BlockingQ中 侦听一个端口是否有来自五个不同用户的传入连接 我们称他们为:

user1 用户2 用户3 用户4 user5

每个人都发送 GPS 数据。

2.第二个线程——将从BlockingQ中读取。 同时,我的java应用程序侦听第二个端口,等待另一个客户端(与发送GPS数据的客户端不同)连接到它。

现在...我有第二个应用程序连接到我刚刚描述的 java 应用程序。

在第二个应用程序中,我有一个列表 user1...user5 ,根据我将选择哪个项目(user1...5),我必须从那里接收正确的数据。

那么现在......我如何写入/读取 BlockingQ 中的数据以便我接收正确的数据???

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 could 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???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一枫情书 2024-11-16 03:28:40

我认为你想要实现的是生产者消费者模式。您应该使用 BlockingQueue< /code> 和一个带有纬度和经度的不可变的 坐标 对象。

坐标 应该只包含经度和纬度两个字段,这两个字段都是final 字段,并且只能使用构造函数进行设置。通过这种方式,您可以确保没有任何竞争条件或使用错误的线程意外更改字段。

BlockingQueue 不会永久存储数据,因此如果您需要这样做,您可以创建一个单例。
更好的解决方案是创建一个像 ShareData 这样的类,然后将它们传递给两个线程,这样您就可以节省创建线程安全单例的时间。这可能看起来像这样:

public interface ShareData {
    public void AddToList(Coordinate coord); // add to BlockingQueue and an internal store here
    public Coordinate TakeFromList();  // take from BlockingQueue
}

Cooperative 类应该看起来像这样并且是线程安全的,因为它是不可变的并且不能更改其值。

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;
  }
}

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;
  }
}
夕嗳→ 2024-11-16 03:28:40

我的建议是不要打扰单身人士。 单例几乎从来都不是一个好主意。如果你不这样做不想要超过一个,只是不要制造超过一个。如果您希望它是全局的,请将类的一个对象声明为全局的(但更好的是,只需选择一个类来拥有它即可。)。

我不了解您的程序,但我很有可能您永远不会看到由于某人无意中创建多个共享数据类对象而导致的错误。然而,正确编码线程安全的单例是一个非常棘手的问题,如果您尝试这样做,很可能会遇到错误。

然后有一天,当您发现由于某种原因需要另一对这些通信线程时,会发生什么?当然,您希望这些共享数据对象中的另一个与它们一起使用,但是因为您将其设置为单例,所以它成为该类的主要重写而不是琐碎的事情。

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.

待天淡蓝洁白时 2024-11-16 03:28:40

只需创建一个类来存储该信息即可。纬度/经度可以用双精度数表示。

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
   }

}

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
   }

}
挽清梦 2024-11-16 03:28:40

并发队列

我建议在 java.util.concurrent 的几种实现可以帮助您:ConcurrentLinkedQueue、ConcurrentBlockingQueue,

这样您就有一个单例队列,一个线程从中读取,另一个线程写入

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文