JTextArea.append 的线程安全 [英] Thread Safety of JTextArea.append

查看:32
本文介绍了JTextArea.append 的线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java 文档说 append 方法是线程安全的.但是,我记得当我尝试从不同的线程(几个月前)使用附加到文本区域时,我得到混乱的文本,其中线程 1 会附加一些字符,而线程 2 会附加一些其他字符.因此,我没有在 jtextarea 中获得 STRINGstring,而是获得了 SstTrRINingG.

The java doc says the append method is thread safe. However, I recall that when I tried using the append to the text area from different threads (several months ago), I get jumbled text where thread 1 would append some characters and thread 2 would append some other characters. So instead of getting STRINGstring in the jtextarea, I get SstTrRINingG.

两者之间会有什么区别:

What differences would there be between:

  1. 同步追加
  2. 瓶颈通过线程池执行器从不同线程追加
  3. 在 EDT 上使用 invokeLater

或者他们都可以解决问题吗?谢谢

or are they all fine for fixing the issue? Thanks

推荐答案

虽然 append() 是线程安全的相对于 EDTappend() 在 Java 7 中是不是.使用 invokeLater() 追加 将按照的顺序进行处理入队.对其他方法的批判性检查需要 sscce.

While append() was thread safe with respect to the EDT, append() in Java 7 is not. Appends using invokeLater() will be processed in the order in which they are enqueued. A critical examination of the other approaches would require an sscce.

这篇关于JTextArea.append 的线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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