简体   繁体   English

Java:如何在另一个扩展Thread的类中调用方法,并且该方法是我的主类中的Thread Array的一部分?

[英]Java : How do I call a method in a different class that extends Thread and is a part of a Thread Array from my main class?

I have a simple main class which listens on a socket, and adds (networked) clients to an array of Threads as they join in ( like a server listening for new chat clients , and adding each client as a thread , and maintaining the list of threads as an array). 我有一个简单的主类,它侦听套接字,并在它们加入时将(网络的)客户端添加到线程数组中(例如服务器侦听新的聊天客户端,并将每个客户端添加为线程,并维护线程列表)。线程作为数组)。

Other things are working, these are the code lines where the server listens and adds a new client to an array of threads called thdRunningClientsPool[] 其他事情正在起作用,这些是服务器侦听并向名为thdRunningClientsPool []的线程数组添加新客户端的代码行。

//add a new ClientHandler type object( ClientHandler class extends Thread) to my Thread Array
public void run() {
............
 thdRunningClientsPool[intRunningClients]= new 
                   Thread( new ClientHandler(clientSocket));
 thdRunningClientsPool[intRunningClients].start();

Now, The ClientHandler class has a method void SendMessage(string Message) 现在,ClientHandler类具有方法void SendMessage(string Message)

How do I send a message using this SendMessage method after a new client is added? 添加新客户端后,如何使用此SendMessage方法发送消息? When I use this 当我用这个

thdRunningClientsPool[intRunningClients].SendMessage("hi");

The SendMessage method is not found. 找不到SendMessage方法。 Java is treating thdRunningClientsPool(intRunningClients) as a thread, not as a object of ClientHandler class and so I can't acces my SendMessage method. Java将thdRunningClientsPool(intRunningClients)视为线程,而不是ClientHandler类的对象,因此我无法访问我的SendMessage方法。

How can I fix this? 我怎样才能解决这个问题? Any advice greatly appreciated :) THxxx 任何建议,不胜感激:) THxxx

First, general advice: use implements Runnable instead of extends Thread . 首先,一般建议:使用implements Runnable而不是extends Thread Will save you a lot of headaches down the road. 将为您免去很多麻烦。

As for your question, you can't call sendMessage on the Thread instance that was passed your instance as an argument. 至于您的问题,您不能在作为参数传递给您的实例的Thread实例上调用sendMessage You must change the code to hold references to your ClientHandler s; 您必须更改代码以保存对ClientHandler的引用; you don't really need the array of outer Thread instances. 您实际上并不需要外部Thread实例的数组。 Just start them and let go of them. 只需启动它们,然后放开它们。

You should also reconsider your choice to start all those threads on your own, this is usually a bad idea because it wastes system resources. 您还应该重新考虑自己启动所有这些线程的选择,这通常是一个坏主意,因为这会浪费系统资源。 However, redesigning that aspect of your code is beyond the scope of your question. 但是,重新设计代码的这一方面超出了您的问题范围。

暂无
暂无

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

相关问题 如果一个方法属于另一个扩展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) 在Java中,如果我调用一个从另一个可运行对象扩展Thread的类,哪个线程将执行? - In Java, if I call a class that extends Thread from another runnable object, which Thread executes? 如果我在java中调用我的主类的函数,从具有自己的线程的其他对象,这个函数将在单独的线程中运行? - If i call a function of my main class in java, from other object which has its own thread, will this function run in separate thread? 如何在Java中从主线程调用线程的方法 - How to call a thread's method from main thread in java 一个Java线程object怎么能从原来的class调用一个方法? - How can a Java thread object call a method from the original class? java如何从扩展线程类获取变量值 - java how to get variable value from extends thread class Java:如何在主类中调用方法,而该方法在另一个类中,该类扩展了抽象类 - Java: How to call upon a method in a main class where the method is in another class which extends an abstract class 如何从后台线程调用Java类 - How to call a Java class from a background thread 为什么扩展线程的内部类的执行线程是主线程? - Why the execution thread for inner class, which extends thread, is the main thread? 如何从扩展线程的类开始在JPanel中创建动画(从一个点移动一个圆圈到另一个点)? - How do I start an animation (move a circle from one point to another) in a JPanel from a class that extends thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM