I am using newFixedThreadPool to simultaneously serve incoming requests to a server socket. For some reason the worker classes serving the clients are not being run in separate threads. When I send multiple requests to the socket, the requests get served sequentially, not simultaneously like they should. I am running OpenJDK 1.8.0_77 if that makes any difference.
This is an excerpt of my main class:
ExecutorService executorService = Executors.newFixedThreadPool(100);
ServerSocket serverSocket = new ServerSocket(5656);
while(true) {
Socket socket = serverSocket.accept();
executorService.execute(new ConnectionHandler(socket));
}
This is my worker class:
public class ConnectionHandler implements Runnable {
private Socket socket;
public ConnectionHandler(Socket socket) {
this.socket = socket;
}
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
BufferedReader reader = null;
PrintWriter writer = null;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
while(true) {
String line = reader.readLine();
if(line == null) break;
writer.println("Echo: " + line);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if(reader != null) reader.close();
if(writer != null) writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
Sorry, I not good with computer.
The problem was that I was echoing many messages in a loop to the socket, but the echo command sends messages sequentially and waits for every message to finish before it sends the next one.
The problem is this call:
executorService.execute(new ConnectionHandler(socket));
The execute method is allowed to force the calling thread to block until the task is complete, or even have the calling thread perform the actual work. Instead, do this:
executorService.submit(new ConnectionHandler(socket));
This will asynchronously run your task using threads in the pool.
Related
I am implementing a Transfer Server program which takes messages from clients (via console input) and then forwards it to some sort of mailbox.
To allow concurrent reception of several messages by different clients, I first created a class that implements the Runnable interface. Each of this class instances will handle the communication with exactly one client:
public class ClientConnection implements Runnable {
//...
//...
#Override
public void run() {
try {
// prepare the input reader and output writer
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);
Message message = new Message();
String request = "";
// read client requests
while ((request = reader.readLine()) != null) {
System.out.println("Client sent the following request: " + request);
String response;
if (request.trim().equals("quit")) {
writer.println("ok bye");
return;
}
response = message.parseRequest(request);
if (message.isCompleted()) {
messagesQueue.put(message);
message = new Message();
}
writer.println(response);
}
} catch (SocketException e) {
System.out.println("ClientConnection: SocketException while handling socket: " + e.getMessage());
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (InterruptedException e) {
System.out.println("Client Connection was interrupted!");
e.printStackTrace();
} finally {
if (clientSocket != null && !clientSocket.isClosed()) {
try {
clientSocket.close();
} catch (IOException ignored) {}
}
}
}
}
I do have a parent thread which is responsible for starting and managing all the ClientConnection runnables:
#Override
public void run() {
clientConnectionExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
while (true) {
Socket clientSocket;
try {
// wait for a Client to connect
clientSocket = serverSocket.accept();
ClientConnection clientConnection = new ClientConnection(clientSocket, messagesQueue);
clientConnectionExecutor.execute(clientConnection);
} catch (IOException e) {
// when this exception occurs, it means that we want to shut down everything
clientConnectionExecutor.shutdownNow(); // force terminate all ClientConnections
return;
}
}
}
Now according to this Stackoverflow Question, I would have expected that as soon as shutdownNow(); is being called, an InterruptedException would be thrown within my ClientConnection.run() method, and there, it should print Client Connection was interrupted!. But this does not happen, so the catch clause seems never to be reached, the input reading loop just goes on.
I read in another Stackoverflow question that this might be related to some other codeline within the block seems to be consuming the InterruptedException, but there wasn't any particular information on what codeline could do that. So I am thankful for any hints.
Edit: It turns out that as soon as I manually exit the loop by typing "quit" on the client, the loop will quit and then, Client Connection was interrupted! will be printed. So somehow the exception seems to be ignored as long as the loop is running, and only handled afterwards.
From Oracle docs for shutdownNow:
There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.
If you take a look into ThreadPoolExecutor sources, you will find out that shutdownNow interrupts threads with this code:
void interruptIfStarted() {
Thread t;
if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
try {
t.interrupt();
} catch (SecurityException ignore) {
}
}
}
Your ClientConnection doesn't check the flag Thread.interrupted. Due to information in the post, I can't figure out which method throws InterruptedException. Probably, some other method, for example, readLine of reader or writer, blocks the thread, because they use socket's InputStream and OutputStream and because it's obvious that socket's streams block the thread if data is not immediatly available.
For example, I wrote this code to test it:
class Example {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
try(ServerSocket serverSocket = new ServerSocket()) {
serverSocket.bind(new InetSocketAddress(8080));
Socket socket = serverSocket.accept();
int dataByte = socket.getInputStream().read();
System.out.println(dataByte);
} catch (IOException e) {
e.printStackTrace();
}
});
thread.start();
thread.interrupt();
}
}
On OpenJdk-16.0.2 there is no actual interruption.
I see two possible solutions for your problem:
Check Thread.interrupted inside the while loop if you are sure that Socket doesn't block your thread.
If your are not sure, use SocketChannel in non-blocking mode instead of Socket for checking Thread.interrupted manually.
For the second way I tranformed my example into this:
class Example {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
try(ServerSocketChannel serverSocket = ServerSocketChannel.open()) {
serverSocket.configureBlocking(false);
serverSocket.bind(new InetSocketAddress(8080));
SocketChannel socket = null;
while (socket == null) {
socket = serverSocket.accept();
if (Thread.interrupted()) {
throw new InterruptedException();
}
}
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
socket.read(byteBuffer);
byte[] bytes = new byte[byteBuffer.limit()];
byteBuffer.flip();
byteBuffer.get(bytes);
System.out.println(new String(bytes, StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println("Interrupted successfully");
}
});
thread.start();
thread.interrupt();
}
}
It works.
Good luck with Java :)
I would have expected that as soon as shutdownNow(); is being called, an InterruptedException would be thrown within my ClientConnection.run()
Your messagesQueue should be a BlockingQueue. So messagesQueue.put(message) will make you need to catch an Interrupted exception. So only when the thread is blocked in the put method(queue is full), you call threadpool#shutdownNow, then the thread will receive an Interrupted exception. In other cases, thread will not receive this Interrupted exception.
You can change while ((request = reader.readLine()) != null) to while ((request = reader.readLine()) != null && !Thread.interrupted()).
Another solution is to maintain all client sockets, and close all client sockets when you need to close them, this way, the client thread will directly receive an IOException:
List<Socket> clientSockets = new ArrayList<>();
while (true) {
try {
Socket accept = serverSocket.accept();
clientSockets.add(accept);
executorService.submit(new ClientConnection(accept));
}catch (Exception e) {
for (Socket socket : clientSockets) {
try {
socket.close();
} catch (Exception exception) {
//
}
}
//executorService.shutdownNow();
}
}
Relevant code:
#Test
public void serverTest() throws IOException {
GameServer server = new GameServer();
server.start(9000);
GameClient client1 = new GameClient();
GameClient client2 = new GameClient();
client1.startConnection("localhost", 9000);
client2.startConnection("localhost", 9000);
client1.sendMessage("Hey I am client 1");
client2.sendMessage("Hey I am client 2");
}
public class GameServer {
private ServerSocket serverSocket;
public void start(int port) throws IOException {
System.out.println("Server started !!!");
serverSocket = new ServerSocket(port);
while (true) {
new Thread(new GameClientHandler(serverSocket.accept())).start();
}
}
public void stop() throws IOException {
serverSocket.close();
}
private static class GameClientHandler implements Runnable {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public GameClientHandler(Socket socket) {
this.clientSocket = socket;
}
#Override
public void run() {
System.out.println(Thread.currentThread().getName());
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine = in.readLine();
while ((inputLine = in.readLine()) != null){
System.out.print(inputLine);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Why can't the server and client be started together in the #Test? I think it gets stuck in the infinite while loop but at the same time, shouldn't there be context switching with the new threads started after accepting the connection?
I expected at least the name of the 2 new threads to be printed but it doesn't happen.
Let us look carefully to your test code:
GameServer server = new GameServer();
Ok, this lines creates a server, and the test thread is ready to execute next line
server.start(9000);
Ok, the test thread starts the server, and will be ready to execute the next line when the start method will return.
What happens in start:
System.out.println("Server started !!!");
Ok, you should see that message
serverSocket = new ServerSocket(port);
Ok, you have created a ServerSocket
while (true) {
new Thread(new GameClientHandler(serverSocket.accept())).start();
}
ok you a waiting for a connection (at serverSocket.accept()), will create a new thread to handle it as soon as you will get one, and loop again.
But as this point, the test thread is waiting and will never go to the following line to start the first connection. And it will remain stuck unless something else (maybe another thread) starts those damned connections.
The method GameServer.start will only return with an exception. That is because you have the while-loop.
So your test execution will start the server and wait for someone to open a connection, but that never happens.
I am trying to create peers that are connected to each other and are able to send/receive messages to everyone that they are connected to concurrently. They are all run on the same machine. Once connecting to a peer and acquiring a socket connection, I am starting two threads, one that is reading and one that is writing. However, readLine() blocks indefinitely and I am not sure where the problem exists. The peers successfully connect to each other, but the message exchange fails. Interrupting one peer causes a "null" message to be read from the other peers connected to it.
EDIT: Using autoflush = true in PrintWriter throws "ConnectionReset" exception to the other peers connected to the interrupted peer.
I have tried waiting for the reading thread to start before sending the message, getting the socket connection from either end of the peers, storing the input/output streams in lists stored as member variables and also just passing only the socket/all streams to each thread. Also tried different ways of reading, shown in comments in the reading thread.
class ConnectionListener extends Thread{
public void run(){
try {
while (!allPeersJoined()) {
Socket socket = null;
socket = peerServer.accept();
new PeerReader(socket).start();
new PeerWriter(socket).start();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
}
class PeerWriter extends Thread{
PrintWriter writer;
public PeerWriter(Socket socket){
try {
this.writer = new PrintWriter(socket.getOutputStream(), true);
}catch (IOException e){
e.printStackTrace();
}
}
#Override
public void run() {
writer.println("Hello");
}
}
class PeerReader extends Thread{
BufferedReader reader;
public PeerReader(Socket socket){
try {
this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch (IOException e){
e.printStackTrace();
}
}
#Override
public void run() {
String input = "";
System.out.println("Waiting to receive messages...");
try {
System.out.println(reader.readLine());
// while((input = reader.readLine()) != null){
// System.out.println(input);
// }
}catch (IOException e){
e.printStackTrace();
}
}
}
EDIT: Adding socket creation
Thread t = new ConnectionListener();
t.start();
// Connect to all peers
for (String peer : peers) {
new Socket("127.0.0.1", Integer.valueOf(peer));
}
You are creating multiple socket but then never use them.
Right now the ConnectionListener get your connection, send the bytes on the connected sockets, but the sockets never responds ... because you never told them to.
You should in the same way you did with ConnectionListener, create an object called
ClientConnection, that has its own writer and reader.
I have a problem with multithreading. I don't know why, but threads run in sequence.
There is a client-server application. I need to run several parallel threads for message exchange. The whole classes are too big, so I will show main parts.
Code of Client(This code runs every time when I push the button):
public ArrayList<Action> call() {
Thread myThready = new Thread(new Runnable()
{
public void run()
{
BufferedReader in;
PrintWriter out;
try{
Socket fromserver = new Socket(ip, PortID);
in = new BufferedReader(new InputStreamReader(fromserver.getInputStream()));
out = new PrintWriter(fromserver.getOutputStream(),true);
writeLog(query, ID, 0);
out.println(query+"ID"+ID);
String fserver = in.readLine();
writeLog(fserver, ID, 1);
out.println("exit");
out.close();
in.close();
fromserver.close();
}
catch (IOException io){
return;
}
}
});
myThready.start();
}
Code of Server:
public void run(){
flag=true;
System.out.println("Welcome to Server side!");
createLog();
ExecutorService service = Executors.newCachedThreadPool();
ServerSocket servers = null;
int n=4600;
try{
servers = new ServerSocket(n);
} catch( Exception e){
}
Socket fromclient = null;
while(true){
try {
System.out.print("Waiting for a client...");
fromclient = servers.accept();
System.out.println("Client connected.");
Callable<ArrayList<Action>> callable = new HandleThread(fromclient);
Future<ArrayList<Action>> future = service.submit(callable);
list.addAll(future.get());
} catch (Exception e) {
System.out.println("Can't accept.");
System.exit(-1);
}
}
}
This code does "accept()" and then creates new thread for some calculations.
Code of HandleThread:
public ArrayList<Action> call() {
BufferedReader in = null;
PrintWriter out= null;
try {
in = new BufferedReader(new
InputStreamReader(fromclient.getInputStream()));
out = new PrintWriter(fromclient.getOutputStream(),true);
String input,output;
System.out.println("Wait for messages.");
while ((input = in.readLine()) != null) {
//close filewriter thread if input==exit
if(input.equalsIgnoreCase("exit")){
break;
}
System.out.println(input);
String[] arr = input.split("ID");
System.out.println("+"+arr[0]);
ID = Integer.parseInt(arr[1]);
writeLog(arr[0], ID, 0);
process(arr[0], ID);
out.println(arr[0]);
writeLog(arr[0], ID, 1);
}
out.close();
in.close();
fromclient.close();
} catch(IOException e){
return null;
}
return list;
}
I don't know why this doesn't work. I have logs and I see that one thread runs only after another one. Not at the same time!
Please, help me!
Future#get() is a blocking call.
list.addAll(future.get());
The calling thread will wait until the task is done. As such, your server thread which calls accept() waits for each task to finish before it gets to the next one.
I'm using socket to writing multiple clients - server application in java. I wrote simple client - server application and everything was ok but when i tried change it to multi clients app I got the exception when i started client:
Exception in thread "pool-1-thread-1" java.lang.NullPointerException
at MiniSerwer.run(Serwer.java:110)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
I have two more classes (threads - InWorker and OutWorker to input and output).
Serwer.java (without InWorker and OutWorker) :
public class Serwer {
Serwer(int port) {
ServerSocket serversocket=null;
ExecutorService exec= Executors.newCachedThreadPool();
try {
serversocket=new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
while(true) {
Socket socket=null;
try {
socket = serversocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
exec.execute(new MiniSerwer(socket)); // create new thread
}
} }
MiniSerwer - helper class to create owns thread to everyone client
class MiniSerwer implements Runnable{
Socket socket=null;
ExecutorService exec=null;
ObjectOutputStream oos=null;
ObjectInputStream ois=null;
MiniSerwer(Socket socket) {
this.socket=socket;
try {
oos=new ObjectOutputStream(socket.getOutputStream());
oos.flush();
ois=new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while(true) {
exec.execute(new InWorker(socket, ois)); // input stream
exec.execute(new OutWorker(socket, oos)); //output stream
Thread.yield();
}
}
}
I change my program but it still doesn't work. Any other suggestion ?
Server:
public class Serwer implements Runnable{
ServerSocket serversocket=null;
ExecutorService exec= Executors.newCachedThreadPool();
int port;
Serwer(int port) {
this.port=port;
}
public void run() {
try {
serversocket=new ServerSocket(port);
while(true) {
Socket socket=null;
try {
socket = serversocket.accept();
exec.execute(new MiniSerwer(socket)); // create new thread
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
int port;
Scanner in = new Scanner(System.in);
System.out.println("Enter the port:");
port = in.nextInt();
ExecutorService exec=Executors.newCachedThreadPool();
exec.execute(new Serwer(port));
}
}
MiniSerwer:
class MiniSerwer implements Runnable{
Socket socket=null;
ExecutorService exec=Executors.newCachedThreadPool();
ObjectOutputStream oos=null;
ObjectInputStream ois=null;
MiniSerwer(Socket socket) {
this.socket=socket;
}
public void run() {
try {
oos=new ObjectOutputStream(socket.getOutputStream());
oos.flush();
ois=new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
while(true) {
exec.execute(new InWorker(socket, ois)); // input stream
exec.execute(new OutWorker(socket, oos)); //output stream
Thread.yield();
}
}
}
I have a lot of exceptions when I'm trying send message from client to server.
2 sec. after connecting on the server side (i don't send anything) :
Exception in thread "pool-2-thread-1" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:657)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:943)
at java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:992)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
Your executor service, in your MiniSerwer class, is never initialized. That's the root cause of your NPE.
But like I mentioned in my comment, you should not be doing all the Serwer logic in it's constructor. The object never gets fully initialized because you never exit the constructor. Make the entire class Runnable and move that logic to the overridden run method. Then add a main method to be used in actually instantiation/running the server.
Also, in your MiniSerwer, your stream initialization can fail. You need to validate the streams aren't null before using them in the run method. Or just move the initialization logic for them to the beginning of your run method.
EDIT
You also have a bug in your MiniSerwer implementation, you spawn an infinite number of threads to process the object input and output streams:
while(true) {
exec.execute(new InWorker(socket, ois)); // input stream
exec.execute(new OutWorker(socket, oos)); //output stream
Thread.yield();
}
You will either run out of threads, run out of memory, or both. To be honest, your solution is over-engineered, I doubt the In and Out workers really need to be separated the way you have them now.
Never do such terrible things. In the code below you ignored exception twice:
First ignore:
try {
serversocket=new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
after that serversocket could be null as before
and second ignore:
while(true) {
Socket socket=null;
try {
socket = serversocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
exec.execute(new MiniSerwer(socket)); // create new thread
}
after that try...catch socket also could be null as before.
Both things could lead to the NullPointerException !
Fix it all like:
try {
serversocket=new ServerSocket(port);
while(true) {
Socket socket=null;
try {
socket = serversocket.accept();
exec.execute(new MiniSerwer(socket)); // create new thread
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
} catch (IOException e) {
e.printStackTrace();
}
UPD: This one: while(true) {...} is also terrible thing. But let's left this after the brackets of this question.