gpt4 book ai didi

java - java中从另一个线程调用方法

转载 作者:行者123 更新时间:2023-12-01 18:09:55 27 4
gpt4 key购买 nike

我编写了一个简单的聊天服务器,但我不知道如何将消息发送到另一个线程。我尝试过的返回错误:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

class ConnectorService extends Thread {

Socket connection;
ObjectInputStream in;
ObjectOutputStream out;
Thread _t;

public ConnectorService( Socket sock, ObjectInputStream _in, ObjectOutputStream _out ) {
this.connection = sock;
try {
in = _in;
out = _out;
out.flush( );
}
catch( Exception e ) {
System.err.println( "Wystapil blad laczenia do socketu.." );
}
}

@Override
public void run() {

this.send( "Siemka :P" );

while( true ) {
try {
String mess = (String)in.readObject( );

String[] m = mess.split( ":" );

if( m.length > 1 ) {
Thread _tmp;
if( ( _tmp = getThreadByName( m[ 0 ] ) ) != null ) {
// Here i've got an exception
( ( ConnectorService ) _tmp ).send( m[ 1 ] );
}
}
else {
System.err.println( "Zbyt malo argumentow.." );
}

this.getThreadByName( "test" );
System.out.println( "Klient: " + mess );
this.send( mess );
}
catch( ClassNotFoundException e ) {
System.err.println( "Bledny typ wiadomosci.." );
}
catch (IOException e) {
// e.printStackTrace();
System.err.println( "Polaczenie przerwane.." );
break;
}
}


}

public void start( ) {
System.out.println( "Uruchamiam klienta" );
if( _t == null ) {
_t = new Thread( this );
_t.start();
}
}

public void send( String message ) {
try {
out.writeObject( message );
out.flush();
}
catch( IOException e ) {
e.printStackTrace( );
}
}

public Thread getThreadByName(String threadName) {
for (Thread t : ConnectorService.getAllStackTraces().keySet()) {
if (t.getName().equals(threadName)) return t;
//System.out.println( t.getName() );
}
return null;
}

}

我尝试强制转换从方法 getThreadByName() 获得的线程,但随后我收到了无法强制转换线程的异常。

是否有可能在另一个线程中调用该方法?问候

最佳答案

就像 alamar 所说,使用消息队列会更好,但由于您将其用于简单的程序,因此这里有一个简单的解决方案:

您需要保留对您创建的每个线程的引用列表,并保留每个线程的名称,也许使用映射:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

class ConnectorService extends Thread {

static Map<String, Thread> connectorServices = new HashMap<>();

Socket connection;
ObjectInputStream in;
ObjectOutputStream out;
Thread _t;

public ConnectorService(String name, Socket sock, ObjectInputStream _in, ObjectOutputStream _out) {
this.connection = sock;
try {
in = _in;
out = _out;
out.flush();
} catch (Exception e) {
System.err.println("Wystapil blad laczenia do socketu..");
}
// add this to the list of references to the threads
ConnectorService.connectorServices.put(name, this);
}

@Override
public void run() {

this.send("Siemka :P");

while (true) {
try {
String mess = (String) in.readObject();

String[] m = mess.split(":");

if (m.length > 1) {
Thread _tmp;
if ((_tmp = getThreadByName(m[0])) != null) {
// Here i've got an exception
((ConnectorService) _tmp).send(m[1]);
}
} else {
System.err.println("Zbyt malo argumentow..");
}

this.getThreadByName("test");
System.out.println("Klient: " + mess);
this.send(mess);
} catch (ClassNotFoundException e) {
System.err.println("Bledny typ wiadomosci..");
} catch (IOException e) {
// e.printStackTrace();
System.err.println("Polaczenie przerwane..");
break;
}
}

}

public void start() {
System.out.println("Uruchamiam klienta");
if (_t == null) {
_t = new Thread(this);
_t.start();
}
}

public void send(String message) {
try {
out.writeObject(message);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}

public Thread getThreadByName(String threadName) {
return ConnectorService.connectorServices.get(threadName);
}
}

这段代码应该可以编译并正常工作......

但请注意我没有使用正确的封装 - 我将把它留给你来做...

关于java - java中从另一个线程调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33792647/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com