简体   繁体   English

如何将对象从主线程传递到java中的另一个线程

[英]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. 它允许您将对象存储到队列中,然后等待它被删除。

  1. Publishing JTextArea reference to another thread is not correct because Swing compoments are MVC-based (model-ui actually). 将JTextArea引用发布到另一个线程是不正确的,因为Swing组件是基于MVC的(实际上是model-ui)。 So, your general way to work with swing components is to update their models and let view (ui) display the change; 因此,使用swing组件的一般方法是更新模型并让view(ui)显示更改;
  2. Another point is that most of swing componenets aren't thread-safe, ie you can't be sure that your application works fine if you use swing component from the thread over than EDT ; 另一点是,大多数摆动组件都不是线程安全的,即如果使用线程中的swing组件而不是EDT ,则无法确定应用程序是否正常工作;

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.

相关问题 如何将对象从Android中的另一个线程传递回主线程? - How to pass the object back to Main thread from another threads in Android? 如何从Java中的另一个线程类更新和使用main()中的对象 - How to update and use an object in main() from another thread class in java 如何从主线程超时 java 线程? - How to timeout a java thread from main Thread? 如何从另一个线程访问线程的对象-Java - How to access a thread's object from another thread - Java Java,如何将对象从工作线程传回主线程? - In Java, how to pass the objects back to Main thread from worker threads? 将一个简单的java对象从一个线程传递到另一个线程的快速方法 - Fast way to pass a simple java object from one thread to another 当主线程在 Java 中终止时如何停止另一个线程? - How to stop another thread when main thread is terminated in Java? 如何将消息从 TimerTask 传递到主线程? - How to pass a message from TimerTask to main thread? Android:如何从主线程创建消息并将其传递给工作线程 - Android: How to create and pass messages to worker thread from main thread 如果一个方法属于另一个扩展Thread的类但从主线程调用,则该方法是由主线程还是子线程执行? (JAVA) - If a method belongs to another class that extends Thread but is called from the main thread, will it be executed by main or a child thread? (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM