[英]How to pass object from main thread to another thread in java
In my main application class I have an object of JTextArea(say txtArea). 在我的主应用程序类中,我有一个JTextArea的对象(比如txtArea)。 I have created two another threads in main_application program.
我在main_application程序中创建了另外两个线程。 The two threads I have created is for reading and writing in serial port.
我创建的两个线程是用于在串行端口中读写。 Now I want to put the serial port read buffer data into an JTextArea object.
现在我想将串口读缓冲区数据放入JTextArea对象。 So I need to pass the JTextArea object created in main_application to read-thread so that read-thread can put the read output to JTextArea.
所以我需要将在main_application中创建的JTextArea对象传递给read-thread,以便read-thread可以将读取输出放到JTextArea中。
But after doing like this I am facing null pointer access problem. 但是这样做后我面临空指针访问问题。 When I check the JTextArea object in main_application its not null but after passing to thread i checked its null.
当我检查main_application中的JTextArea对象时它不是null但是在传递给线程后我检查了它的null。 I dont know how it happens.... if any geek can help me i will be happy....
我不知道它是怎么发生的......如果有任何极客可以帮助我,我会很高兴....
Thanks , Surjya 谢谢,Surjya
As mentioned in another response, you can only change the contents of a Swing component on the swing thread itself. 如另一个响应中所述,您只能在swing线程本身上更改Swing组件的内容。 A good way to ensure that all your other threads do this is to not expose the actual JTextArea, but rather to provide the other objects with a safe method to add text to the text area from your main_application, like so:
确保所有其他线程执行此操作的一种好方法是不公开实际的JTextArea,而是为其他对象提供一种安全方法,以便从main_application向文本区域添加文本,如下所示:
public class main_application {
private JTextArea txtArea; public void addText(final String txt) { SwingUtilities.invokeLater(new Runnable() { public void run() { txtArea.setText(txtArea.getText() + txt); } }); }
}
This is a simple example, but you can expand on this based on your needs. 这是一个简单的示例,但您可以根据需要对此进行扩展。 This also uses a more costly way of adding to the text since it appends two immutable strings.
这也使用了更昂贵的方式添加到文本,因为它附加了两个不可变的字符串。 You could use JTextArea.getDocument() to retrieve the model and then use the mutation methods in the document to more efficiently update the text.
您可以使用JTextArea.getDocument()来检索模型,然后使用文档中的变异方法来更有效地更新文本。 However, by providing a method like this you can change the implementation in the future without affecting your other threads or objects.
但是,通过提供这样的方法,您可以在将来更改实现,而不会影响其他线程或对象。
Note that the parameter txt is final which is required so that it can be directly referenced in the anonymous inner class. 请注意,参数txt是final,这是必需的,因此可以在匿名内部类中直接引用它。
You may want to decouple this from your main application class and create an interface or class for the method(s)/way(s) you want to manipulate the text area from your other threads and then hand them an object that wraps this text area. 您可能希望将其与主应用程序类分离,并为要从其他线程操作文本区域的方法/方法创建接口或类,然后将它们交给包装此文本区域的对象。
Sharing objects between threads can get very messy, you may want to read about the Actor model for a different approach. 在线程之间共享对象可能会非常混乱,您可能希望阅读有关不同方法的Actor模型 。 Specifically in the GUI arena, an Model-View-Controller approach code help out.
特别是在GUI领域, 模型 - 视图 - 控制器方法代码帮助。
But back the the question, you could quickly hack it by making your JTextArea a static variable and just letting all the different threads modify the static instance at will: 但回到这个问题,你可以通过让你的JTextArea成为一个静态变量并让所有不同的线程随意修改静态实例来快速破解它:
public static JTextArea TXTAREA
void main_application(){
//set up app
...
TXTAREA = new JTextArea()
...
}
then in your serial reading thread, set the text for TXTAREA
. 然后在你的序列阅读线程中,设置
TXTAREA
的文本。 This solution is not ideal, I think a better approach would be to use an MVC approach and have a controller which accepts the value to put in the text area. 这个解决方案并不理想,我认为更好的方法是使用MVC方法并拥有一个接受值放入文本区域的控制器。
Take a look at SynchronousQueue: 看看SynchronousQueue:
http://java.sun.com/javase/6/docs/api/java/util/concurrent/SynchronousQueue.html http://java.sun.com/javase/6/docs/api/java/util/concurrent/SynchronousQueue.html
It will allow you to store an object into the queue and will then wait until it is removed. 它允许您将对象存储到队列中,然后等待它被删除。
So, you should do the following: every time you have new data to display submit new task of JTextArea content update to be executed from the EDT. 因此,您应该执行以下操作:每次有新数据显示时,都要提交要从EDT执行的JTextArea内容更新的新任务。 You can achieve that via SwingUtilities.invokeLater()
你可以通过SwingUtilities.invokeLater()实现这一点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.