0
我正在致力於Distributed System,當前狀態是更多的客戶端/服務器應用程序,因爲我錯過了使其成爲分佈式系統的關鍵部分。我不知道如何實現我的客戶端線程類c_thread將「消息」傳遞給正在運行的所有工作線程w_thread。傳遞給多個線程的消息
import java.net.*;
import java.io.*;
public class w_thread extends Thread {
private Socket socket = null;
private tracker track = null;
private int tID;
//Constructor method
public w_thread(tracker t, Socket s) {
super("w_thread");
this.track = t;
this.socket = s;
tID = track.add();
}
public void run() {
try {
String inputLine, outputLine;
PrintWriter worker_out;
BufferedReader worker_in;
//set up IO to worker
worker_out = new PrintWriter(socket.getOutputStream(), true);
worker_in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Get inputLine from c_thread?
//...
/*
worker_out.close();
worker_in.close();
socket.close();
*/
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class c_thread extends Thread {
private Socket socket = null;
private tracker track = null;
//Constructor method
public c_thread(tracker t, Socket s) {
super("c_thread");
this.track = t;
this.socket = s;
}
public void run() {
try {
String inputLine, outputLine;
PrintWriter client_out;
BufferedReader client_in;
//set up IO to client
client_out = new PrintWriter(socket.getOutputStream(), true);
client_in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
inputLine = client_in.readLine();
if (track.numOfConnections() == 0) {
outputLine = "No resources available!";
}
else {
//send inputline to all w_threads
outputLine = "Resources was available!";
}
client_out.println(outputLine);
/*
client_out.close();
client_in.close();
//close connection
socket.close();
*/
} catch (IOException e) {
e.printStackTrace();
}
}
}
我剛纔的問題介紹了什麼我想實現:Distributed System