在线程之间共享数据 [英] sharing data between threads

查看:127
本文介绍了在线程之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个不同线程的java应用程序,它执行以下操作:

1.第一个线程(它是一个ThreadPool) - 侦听一个端口来自五个不同用户的传入连接。



让我们给他们打电话:

user1 user2 user3 user4 user5



它们中的每发送GPS数据。



2,第二线程
在同一时间我的Java应用程序监听到另一个客户端连接到它(从那些谁发送GPS数据不同)等待第二个端口。



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



在本次应用程序,我有一个列表用户1 ... USER5,并根据所项目我会选择(USER1。 .5)我必须从我选择的用户那里收到正确的数据。
也所有这些数据将被存储在一个数据库的第二个用户。



现在,任何人都可以给我的,我怎么能共享之间的所有数据的提示螺纹????



我试着用辛格尔顿类,并与BlockingQ,但似乎这些都不是合适的导致数据丢失!

$ b $ Java中的并发执行通常依赖于共享内存,因此只要确保两个线程中的代码共享对一个公共数据结构的引用即可可以交换信息。

您需要确保访问此结构是以同步/线程安全的方式完成的。这可以通过使用 synchronized 关键字(不推荐)或通过使用 java.util.concurrent 包(推荐)。



A BlockingQueue 可能适合你。你尝试这个类时遇到了什么问题?


问题是,从BlockingQueue读取的线程需要区分数据这是写在BlockingQ(数据来自user1,2,3 ...)。




我建议你创建一个类对于 UserData ,它包含数据和来自哪个用户。 (并将其存储在 BlockingQueue< UserData> 中。)


BlockingQ可以保存多少数据?因为所有这些数据都需要存储在我的第二个应用程序中的数据库中......所以我可以承受丢失的任何数据!!!!!!!!!!! p>



实际上是一个接口,但它的所有标准实现( ArrayBlockingQueue LinkedBlockingQueue ...)都可以保留任意数量的数据(即仅受限于free内存在你的电脑上)。


I have a java app with two different threads that does the following thing:

1.The first thread(which is a ThreadPool)- 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 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 depending on which item I will choose(user1...5) I have to receive the correct data from the user I pick. Also all this data will be stored at the second user in a DB.

Now can anyone give me a hint of how could I share all this data between the threads ????

I tried with Singleton classes and also with BlockingQ but seems none of these are suitable cause the data gets lost!

解决方案

Concurrent execution in Java generally rely on "shared memory" so just make sure the code in the two threads share a reference to a common data structure in which they can exchange information.

All you need to make sure is that the access to this structure is done in a synchronized / thread safe manner. This can be done manually by using the synchronized keyword (not recommended) or by using classes from the java.util.concurrent package (recommended).

A BlockingQueue would probably suit you well. What problems did you have when you tried this class?

The thing is that the thread that reads from the BlockingQueue needs to differentiate between the data that is written in the BlockingQ(data that comes from user1,2,3...).

I suggest you create a class for UserData which contains both the data and which user it came from. (And store it in a BlockingQueue<UserData>.)

How long and how much data can a BlockingQ keep????Because all this data needs to be stored in a DB in my second app...so I can afford losing any of it!!!!!!!!!!!

The BlockingQueue is actually an interface, but all standard implementations of it (ArrayBlockingQueue, LinkedBlockingQueue...) can all keep an arbitrary ammount of data (i.e., are limited only by the ammount of free memory on your computer).

这篇关于在线程之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆