Java 将多个对象从子线程传递到主线程
我有这个问题,我有一个多线程服务器运行。下面是代码:Java 将多个对象从子线程传递到主线程,java,multithreading,Java,Multithreading,我有这个问题,我有一个多线程服务器运行。下面是代码: ServerSocket serverSocket=null; // defining a server socket to listen data Socket clientSocket = null; // defining a client socket to send data final int port=8080; int i=0; try {
ServerSocket serverSocket=null; // defining a server socket to listen data
Socket clientSocket = null; // defining a client socket to send data
final int port=8080;
int i=0;
try {
serverSocket = new ServerSocket(port); // Opening a server socket to listen for client calls
System.out.println("Server started.");
} catch (Exception e) {
System.err.println("Port already in use.");
System.exit(1);
}
while (true) {
try {
clientSocket = serverSocket.accept(); //binding server socket to client socket incoming call and accepting call
System.out.println("Accepted connection : " + clientSocket);
i=i+1;
Thread t = new Thread(new newClientHandler(clientSocket, NodePRs[1]),"thread"+i); //Create a new thread to handle the single call coming from one client
System.out.println("Thread "+t.getName()+" is starting");
t.start(); //Starting the run method contained in newCLIENTHandler class
} catch (Exception e) {
System.err.println("Error in connection attempt.");
}
}//end while
我所需要的就是让子线程(每次客户端请求出现时打开)在子线程死亡时传递(像函数返回)4个变量。新的ClientHandler代码如下:
public class newClientHandler implements Runnable {
private final static int FILE_SIZE=6022386;
private Socket clientSocket;
private PaillierPrivateKey PrivKey;
ServerSocket servSock;
BigInteger[] msg = null;
BigInteger preamble = null;
int bytesRead;
int current = 0;
DataOutputStream dos = null;
BufferedReader dis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
int msgtype=-1;
int num_of_rx_cnks=-1;
public newClientHandler(Socket client, PaillierPrivateKey PR) {
this.clientSocket = client;
this.PrivKey = PR;
}
//I CAN RECEIVE 3 TYPES OF MESSAGES: SHARE, THE ENCRYPTED PASSWORD, THE 4 PDMS
public void run() {
try{
ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
preamble = (BigInteger) ois.readObject();
System.out.println("Received Preamble is:"+preamble);
oos.writeObject("Received preamble");
msg =(BigInteger[]) ois.readObject();
System.out.println("Received Message is:"+msg+"\n"+msg[0]+"\n"+msg[2]);
String sPlain = Utilities.bigIntegerToString(preamble);
String[] splitArr=Pattern.compile("-").split(sPlain);
msgtype=Integer.parseInt(splitArr[0]);
num_of_rx_cnks=Integer.parseInt(splitArr[1]);
System.out.println("Message type: "+msgtype+"\n"+"Number of received cnks: "+num_of_rx_cnks);
//a questo punto ho i miei 29 biginteger. Li devo sistemare uno accanto all'altro e rimettere nel file.
switch(msgtype){
case 1: //Share received
System.out.println("Received the share");
for(int i=0;i<num_of_rx_cnks;i++){
String name = new String();
if(i<9){
name="Cyph2"+".00"+(i+1);
}
if(i>8){
name="Cyph2"+".0"+(i+1);
}
Utilities.newBigIntegerToFile(msg[i], name);
}
Utilities.retrieveShare(PrivKey, 2,"myShare");
int l, w;
BigInteger v, n, shares, combineSharesConstant;
BigInteger[] viarray=new BigInteger[5];
PaillierPrivateThresholdKey[] res = null;
try {
FileReader File= new FileReader("myShare");
BufferedReader buf=new BufferedReader(File);
String line=buf.readLine();
l = Integer.parseInt(line.split(":")[1]);
line = buf.readLine();
w = Integer.parseInt(line.split(":")[1]);
line = buf.readLine();
v = new BigInteger(line.split(":")[1]);
line = buf.readLine();
n = new BigInteger(line.split(":")[1]);
line = buf.readLine();
combineSharesConstant = new BigInteger(line.split(":")[1]);
line = buf.readLine();
shares = new BigInteger(line.split(":")[1]);
for(int i=0; i<5; i++){
line = buf.readLine();
viarray[i] = BigInteger.ZERO;
}
SecureRandom rnd = new SecureRandom();
PaillierPrivateThresholdKey result = new PaillierPrivateThresholdKey(n, l, combineSharesConstant, w, v,
viarray, shares, 2, rnd.nextLong());//il 2 qua è il nodeID
}catch(IOException e){
System.out.println(e);
}
break;
case 2: // Session Secret received
break;
case 3: //PDM received
break;
}//end switch
}catch(IOException ioe){
System.out.println(ioe);
}catch(ClassNotFoundException cnfe){
System.out.println(cnfe);
}finally {
try{
if (dis != null) dis.close();
if (dos != null) dos.close();
if (clientSocket!=null) clientSocket.close();
}catch (IOException e){
System.out.println(e);
}
}
}
}
公共类newClientHandler实现可运行{
私有最终静态int文件_SIZE=6022386;
私有套接字clientSocket;
私人配对私钥;
serversocketservsock;
BigInteger[]msg=null;
BigInteger前导码=null;
int字节读取;
int电流=0;
DataOutputStream dos=null;
BufferedReader dis=null;
FileOutputStream=null;
BufferedOutputStream bos=null;
int msgtype=-1;
接收cnks的整数=1;
public newClientHandler(套接字客户端,PaillierPrivateKey PR){
this.clientSocket=客户端;
this.PrivKey=PR;
}
//我可以收到3种类型的消息:共享、加密密码和4个PDM
公开募捐{
试一试{
ObjectOutputStream oos=新的ObjectOutputStream(clientSocket.getOutputStream());
ObjectInputStream ois=新的ObjectInputStream(clientSocket.getInputStream());
序言=(BigInteger)ois.readObject();
System.out.println(“接收到的序言是:“+序言”);
oos.writeObject(“收到的序言”);
msg=(BigInteger[])ois.readObject();
System.out.println(“收到的消息是:“+msg+”\n“+msg[0]+”\n“+msg[2]);
String sPlain=Utilities.bigIntegerToString(序言);
String[]splitArr=Pattern.compile(“-”).split(sPlain);
msgtype=Integer.parseInt(splitArr[0]);
num_of_rx_cnks=Integer.parseInt(splitArr[1]);
System.out.println(“消息类型:“+msgtype+”\n“+”接收的CNK数:“+num\u接收的CNK数”);
//这是一个29个大整数的文件,它是一个完整的文件。
开关(msgtype){
案例1://收到的份额
System.out.println(“收到股份”);
对于(inti=0;i使用可调用而不是可运行
将这4个变量绑定到一个类中并实现Callable
。与执行器一起运行Callable,您将在将来得到结果
首先,给变量起个好名字。l
、w
、v
和n
都不是好名字。其次,声明等于null
重新赋值是一个很坏的习惯。在声明时分配它们,并声明它们是“代码>最终< /代码>,这样就可以打破这个习惯。最后,不要为每一个请求产生一个<代码>新的线程< /代码>。这是慢的和容易出错的。考虑一个。回答你的主要问题。为什么<代码>主< /代码>线程保护?第一件事,THX。对于提示,我将进行修改。由于我是自学成才,我错过了一些东西,因此感谢您指出:)。事实是:我必须在5个节点上运行此服务器,在MANET环境中相互通信,4个节点必须与其他节点通信,因此我必须为每个客户端请求打开一个子线程,让主线程可以自由接收来自其他客户端的数据。为此,我必须将这些变量传递给我的主线程,然后将它们再次传递给一个处理线程,让它进行一些处理,比如加密和解密消息和文本。我必须修改我的所有代码,难道没有办法传递一个对象,比如PaillierPrivateThresholdKey结果吗?比如在主线程中声明一个空PaillierPrivateThresholdKey,然后将它传递给线程,然后修改它主电路中的ed?(具有适当的同步化)您可以将结果存储在任何您喜欢的地方。您可以将结果放入主线程使用的主对象中。但是,我不确定主线程如何知道它们何时准备就绪。为此,您需要对代码进行更重要的更改。好的,非常感谢。我将尝试了解什么是可调用对象并修改我的工作:)rly appreci爱德