我有这个问题,我有一个多线程服务器正在运行。这是代码:
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个变量,当子线程死亡时。 newClientHandler代码是这样的:
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);
}
}
}
}
我想传递l,w,v,n等等让我的主线程做一些处理。如何修改我的代码呢?
答案 0 :(得分:0)
使用Callable而不是Runnable。
将4个变量捆绑到一个类中并实现Callable<YourNewClass>
。使用执行程序运行您的callable,您将获得Future<YourNewClass>
中的结果。