Java中可扩展的多线程String对象

发布于 2024-11-25 10:34:33 字数 176 浏览 3 评论 0原文

我正在对我计划构建的服务器应用程序进行一些研究。 主要功能是 - 许多用户将能够执行实时编辑。

因此,研究可扩展字符串的所有选项,这些选项基本上是某种字符串缓冲区,但能够处理大量(数百个?)同时处理它的线程和大量文本。

我希望看到具有此类功能的共享库,而不是重新发明轮子:) 我用谷歌确实找不到太多东西。

I'm doing a little research for a server application I'm planning to build.
The main function will be - lots of users will be able to perform live editing.

So looking into all the options for a scalable string, which basically be some kind of stringbuffer but be able to handle lots(hundreds?) of threads working on it at the same time with quite a large amount of text.

Instead of reinventing the wheel, I hope to see libraries shared that have such features :)
I couldn't really find much with Google.

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

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

发布评论

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

评论(6

夜未央樱花落 2024-12-02 10:34:33

您可能想查看 Etherpad 的源代码 - 它是一个基于 Java 的协作文本编辑 Web 应用程序,因此它必须有某种字符串实现,允许对字符串的不同区域进行并发写入访问,大概不会丢失数据。当然,能否满足你的性能要求又是另一回事了……

You might want to take a look at the sourcecode of Etherpad - it's a Java-based collaborative text editing web app, so it has to have some sort of string implementation that allows concurrent write access to separate regions of the string, presumably without losing data. Of course, whether it fulfills your performance requirements is a different matter...

埋情葬爱 2024-12-02 10:34:33

字符串本身是线程安全的(因为它是不可变的)并且对于大多数用例来说性能相当高。

String 的主要性能问题是 O(n) 中用于突变的字符串长度(由于需要获取完整副本)。

如果您需要处理很长的字符串,您可能需要使用 Rope 数据结构。 Java 中有多种可用的实现:

  • Ropes for Java - 似乎是一个非常好的实现
  • 我自己的实现 - 这是不太一般,但可能是由于其设计开销非常低,因此实施的操作速度更快。

上面的两个 Rope 实现都符合 CharSequence 接口(String 也实现了该接口),因此,如果您将应用程序设计为使用 CharSequences 而不是 Strings,那么您可以从 Strings 开始,然后在以下情况下切换到 Ropes:你决定你需要它们。

String itself is thread-safe (since it is immutable) and fairly high-performance for most use cases.

The main performance issue with String is that in O(n) in the length of the string for mutations (due to the need to take a complete copy).

If you need to deal with very long strings you probably want to use a Rope data structure. There are several implementations available in Java:

  • Ropes for Java - seems a very good implementation
  • My own implementation - which is less general but might be faster for the implemented operations as it is designed for very low overhead.

Both of the Rope implementations above conform to the CharSequence interface (which String also implements), so if you design your application to work with CharSequences instead of Strings then you can start with Strings and switch to Ropes later if you decide you need them.

尘世孤行 2024-12-02 10:34:33

此演讲(需要免费注册)描述了这些类型介绍了 Google Docs 等用于多用户编辑的算法,并演示了一个简单的实现。在 Scala 中但适用于任何语言。

编辑:哎呀,有点晚了!无论如何可能对某人有用......

This talk (free reg. required) describes the kinds of algorithms used by the likes of Google Docs etc for multi-user editing, and demonstrates a simple implementation. In Scala but applicable to any language.

Edit: oops, a bit late! Might be useful to someone anyway...

你げ笑在眉眼 2024-12-02 10:34:33

嗯, StringBuffer 是线程安全的。您可以以此为基础构建您的系统。

Well, StringBuffer is thread-safe. You could base your system on that.

∞觅青森が 2024-12-02 10:34:33

在我看来,这将是一个挑战。您需要允许个人用户“锁定”他们正在处理的字符串部分,而其他用户可以锁定并处理其他部分。所以基本上你所说的是数据库的一种形式。

您或许可以使用 SQL 来完成此操作,但您必须发明一个协议。

It seems to me that it would be a challenge. You need to allow individual users to "lock" the parts of the string they're working on, while others can lock and work on other parts. So basically what you're talking about is a form of database.

You could probably do it with SQL, but you'd have to invent a protocol.

零時差 2024-12-02 10:34:33

主要问题是跟踪更改,因为您不再依赖索引,这使事情变得困难。

所以我目前正在考虑将字符串存储在 LinkHashMap 或 ListOrderedMap 之类的东西中。但仍在对正确的数据结构进行更多研究...

编辑:在这个阶段,我将使用 ListOrderedMap 来存储我的字符串,看看效果如何...

The main issue was keep track of changes since you no longer rely on the index, it made things difficult.

So I'm currently looking at storing the strings in something like a LinkHashMap or ListOrderedMap now. But still doing more research in the correct data structure ...

Edit: At this stage I will go with a ListOrderedMap to store my strings in and see how that goes...

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