简体   繁体   English

如何从另一个线程访问线程的对象-Java

[英]How to access a thread's object from another thread - Java

I have a specific class which extends Thread. 我有一个扩展Thread的特定类。 In my main i create a number of threads of that class. 在我的主要工作中,我创建了该类的多个线程。 Each thread creates an object of class Hero (a class created by me). 每个线程都会创建一个Hero类的对象(我创建的一个类)。

Is there a way that one thread can access the object of the class Hero of another thread? 有一种方法可以使一个线程访问另一个线程的Hero类的对象? I want to have a way to compare two objects of class Hero of two different threads, either having a method tha can have as arguments the two objects of the two different threads OR by having a way to pass values of the object of one thread to another thread. 我想有一种方法可以比较两个不同线程的Hero类的两个对象,要么具有方法tha可以将两个不同线程的两个对象作为参数,要么可以通过一种方法将一个线程的对象的值传递给另一个线程。

What you could do is have it so that the Class that extends Thread accepts an object of Class Hero in its constructor, like so: 您所能做的就是让扩展线程的类在其构造函数中接受类英雄的对象,如下所示:

public class YourClass extends Thread{
     private Hero hero;

     YourClass(Hero hero){
          this.hero= hero;           
         }
 } 

This way you can create the object first and add it to an ArrayList, before passing the reference to the constructor of the new thread object: 这样,您可以先创建对象并将其添加到ArrayList中,然后再将引用传递给新线程对象的构造函数:

ArrayList heroes= new ArrayList<Hero>();//this should be declared at the class level
Hero hero= new Hero();
heroes.add(hero);
YourClass thread= new YourClass(hero);

The ArrayList of Hero objects is now available to all your threads, and you can write a method which iterates through it to find the object you are looking for. 现在,您的所有线程都可以使用Hero对象的ArrayList,并且您可以编写一个遍历该对象的方法以查找所需的对象。 How you do this is largely down to what it is exactly you want to do. 您如何执行此操作很大程度上取决于您实际要执行的操作。

So... as others have said you can just put all your Hero objects in a static list, dictionary or other structure that everyone can access. 所以...正如其他人所说,您可以将所有Hero对象放入一个静态列表,字典或其他任何人都可以访问的结构中。

BUT its sounds to me like what you are trying to do actually is create Active Objects http://en.wikipedia.org/wiki/Active_object 但对我来说,这听起来像您实际上想要做的是创建活动对象http://en.wikipedia.org/wiki/Active_object

When you have Active objects, each Active Object has its own thread that is tightly wound around its own processing loop. 当您拥有Active对象时,每个Active Object都有自己的线程,该线程紧密地缠绕在其自己的处理循环中。 That loop is fed by a queue of "messages" that come in from other objects. 该循环由来自其他对象的“消息”队列提供。

SO each object stores a reference to other objects it wants to talk to, not a reference to the threads. 因此,每个对象都存储对它要与之交谈的其他对象的引用, 而不是对线程的引用。 The threads are hidden inside the object. 线程隐藏在对象内部。 Each object only exposes one method to other objects -- "QueueMessage", which puts messages for processing on that queue. 每个对象仅向其他对象公开一个方法-“ QueueMessage”,该消息将要处理的消息放在该队列中。

That queue must be implemented as a thread-safe data structure. 该队列必须实现为线程安全的数据结构。 The easiest way to do that in Java is with a ConcurrentLinkedQueue 在Java中最简单的方法是使用ConcurrentLinkedQueue

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM