Reputation: 75
I am trying to create a messaging application. I have a class called MessageServer
and I intend to connect clients to this class.
Every Client
class implements Runnable
.
My approach is to create an ExecutorService
inside the MessageServer
class, and execute every client from here.
My question is, can I execute infinite amount Runnable
's depending on the Executors.newFixedThreadPool(x)
or do they count as individual threads?
I am open to new approaches if possible. I also start the MessageServer thread from main method.
MessageServer
public class MessageServer extends Server implements Runnable{
static LinkedList<Message> messages = new LinkedList<>();
LinkedList<Client> clients = new LinkedList<>();
ExecutorService clientPool = Executors.newFixedThreadPool(3);
public MessageServer() throws IOException {
super(Vars.MESSAGE_PORT);
}
public void addToMessages(Message message) {
if (!messages.contains(message)) {
messages.add(message);
}
}
@Override
public void run() {
while(true){
try {
Socket client = serverSocket.accept();
//Read Client data then add to the list
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
Client current = (Client)in.readObject();
clients.add(current);
} catch (Exception ex) {
Logger.getLogger(MessageServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Super class Server
public class Server{
InetAddress address;
LinkedList<Client> clients = new LinkedList<>();
protected ServerSocket serverSocket;
public Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(Vars.SERVER_TIMEOUT_MS);
//ip = Utilities.getIp();
address = serverSocket.getInetAddress();
}
}
Client
public class Client implements Serializable {
protected String nickname;
protected long id;
protected int key;
// MessageServer
protected transient ObjectOutputStream msgOut;
protected transient ObjectInputStream msgIn;
protected transient Socket messageSocket;
protected transient Socket videoSocket;
public Client(String nickname){
this.nickname = nickname;
createid();
makeConnection();
key = (int) (Math.random() * 8999) + 1000;
}
void makeConnection(){
try {
messageSocket = new Socket(Vars.IP, Vars.MESSAGE_PORT);
//TODO make connection with videoServer
msgOut = new ObjectOutputStream(messageSocket.getOutputStream());
msgIn = new ObjectInputStream(messageSocket.getInputStream());
msgOut.writeObject(this);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
final void createid(){
id = 3;
id = 13 * id + Objects.hashCode(this.nickname);
}
// Getters-setters-hashcode-equals
Upvotes: 0
Views: 189
Reputation: 4743
My question is, can I execute infinite amount
Runnable
s depending on theExecutors.newFixedThreadPool(x)
or do they count as individual threads?
The thread pool with fixed size has internally a queue where tasks are queued. Each Runnable
is put into that queue. A thread takes a new task from the queue if it has finished its current task.
There is no limit of tasks you can queue, except hardware limits. When you queue e.g. 20 tasks and have 10 threads in the pool, only 10 of those tasks, or rather Runnable
s, are executed. In your case, only x
clients would be running because there are only x
threads in the pool with fixed size.
You should use Executors.newCachedThreadPool()
in your case. This thread pool doesn't have a limited size, which enables the pool to run all clients at the same time.
Upvotes: 2
Reputation: 140514
My question is, can I execute infinite amount Runnable's depending on the Executors.newFixedThreadPool(x) or do they count as individual threads?
You can add far more runnables than threads. They are stored in a queue, and executed when a thread is available.
In the Javadoc of the Executors.newFixedThreadPool
method:
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue.
So, yes, it's "infinite".
Upvotes: 2