Java multithreaded server logic, synchronized keyword, problem

I am creating a client-server application in java that will allow several people using a client-side swing application (notepad) to connect to the server. After connecting, each client will have to request control over the notebook so that he can edit it, and then give up control by sending his result to display on the panel all other clients.

The main problem that I encountered is multi-threaded server logic using an instance of the main server and several threads, each of which handles communication with the client.

Iโ€™m not sure if the structure I selected will work or if there will be some problem related to a lack of understanding of how streams work, which leads to data corruption or any other problem related to the stream.

In any case, this is the server code, I was wondering, can someone tell me if this system will work without errors? Of course, add more logic, for example, a cap on the number of connections, a waiting list for blocking, etc., but I'm mainly connected with the connection between threads.

I am also wondering how to access the server instance methods from within the stream, as I am not sure. -number, this has been clarified, I use a common โ€œlockโ€ object, which has a list of thread instances, and each thread instance has a lock instance, so they can call methods on eachother.

Thank you very much.

import java.net.*; import java.io.*; import java.util.ArrayList; public class server { private ArrayList<ClientServiceThread> SocketList; private int lock = 0; private ServerSocket myServerSocket; private Socket mySocket; public static void main(String[] args) { server myserver = new server(); } public server() { /** * This will (when finished) accept only a certain number of connections, * and will then finish the constructor by breaking the while loop. It will * then sit here waiting for the synchronised methods to be called by its worker * threads. */ try{ myServerSocket = new ServerSocket(8080); }catch(Exception e) { System.out.println("Could not create serversocket "+e); } int id = 1; while(true) { try{ mySocket = myServerSocket.accept(); ClientServiceThread cliThread = new ClientServiceThread(mySocket, id); SocketList.add(cliThread); id++; cliThread.start(); }catch(Exception e) { System.out.println("Problem with accepting connections"); } } }//end constructor public synchronized boolean acquireLock(int id) { /** * Here any spawned thread can try to acquire the lock, * so it can be the one to send the data (synchronised to prevent data corruption) */ if(this.lock == 0){ this.lock = id; return true; } else { return false; } } public synchronized void releaseLock(int id) { /** * Any thread can call this, releasing the lock. of course, the lock will only be * released if the thread calling it actually owns the lock. */ if(id == this.lock) { this.lock = 0; } else { //do nothing } } public synchronized void publish(String toSend) { /** * When a thread in control of the lock wants to publish to all other threads, it * invokes this method, which then calls another synchronised method on each thread * in the list, telling it to write to it client with the new data. */ for(int i = 0; i<this.SocketList.size(); i++) { if(i != this.lock) { this.SocketList.get(i).sendData(toSend); } } } } class ClientServiceThread extends Thread{ Socket mySocket; int id; boolean hasControl = false; public ClientServiceThread(Socket mySocket, int id) { /** * this constructor gives it the ID and the socket for communication, it will * then be run */ this.mySocket = mySocket; this.id = id; } @Override public void run() { //listen, it will be either a request, or some data //based on whether the client is the one in control or not (hasControl) try{ //create buffered reader if(!this.hasControl) { //it has control, so wait for the lines } else { //read in one line and then call acquire lock because we know //that it has sent a request for control // how do i access the original class for acquireLock();? } }catch(IOException e) { System.out.println("Problem reading from the socket"); } } public synchronized void sendData(String toSend) { //create writer and send to my client, saying "true" or some other message //the client will recognise as the go-ahead to edit the data. } } 
+4
source share
1 answer

Most likely, you'd better use something like MINA instead of riding yourself. Throw client commands in a parallel queue and process them one at a time, so you donโ€™t have to worry about synchronization.

As an alternative, consider using a RESTful interface instead of sockets (or something other than an applet, such as Ext JS).

+2
source

Source: https://habr.com/ru/post/1379810/


All Articles