1

Hello all I am writing server socket inside my Android app. It should allow only one client. Interesting thing is that I want my server to send messages(strings) to client while they are available. I have queue of strings from which i want my server try taking strings on by one and send to the client, if queue empty do not send and as long as some string is pushed into queue send it. Here is how I do that:

public class Resource {
    public static Queue<String> q = new LinkedList<String>();


    public static synchronized void addString(String commands) {//for reader
        semaphore.add(commands);
    }

    public synchronized String getString() {
        if (!semaphore.isEmpty()) {
            return semaphore.remove();
        } else return "0";

    }
}

For that I have a queue q of string where I can add strings from another class. Here is my server code:

 new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        // Listens for a connection to be made to this socket.
                        Socket socket = my_serverSocket.accept();
                        //send message to client
                        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);                            
                        out.println("1");

                        BufferedReader in = new BufferedReader(new InputStreamReader(socket
                                .getInputStream()));
                        String msg = in.readLine();
                        System.out.println("message is: " + msg);

                        // tidy up
                        //in.close();
                        //socket.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    } catch (SecurityException se) {
                        se.printStackTrace();
                    }
                }
            }
        }).start();
Here I want my serve check if queue is empty, if not get one string and send to the client, and repeat this process infinetely. It is something like producer consumer problem. Also I am interested do i need to use synchronized. Is it possible that i will end up with race condition.

1 Answer 1

1

This is the typical way to implement a producer-comsumer pattern: the producer will put() a string in the queue, the server/consumer will take() a string from the queue. If the queue is empty the server will block and wait, optionally with a timeout.

You probably don't need to bound the queue, so you could instantiate a LinkedBlockingQueue with its empty constructor.

All implementations of BlockingQueue are thread-safe, so you don't need additional synchronisation for their basic operations.1


1. If you need atomicity and/or other memory effects, you must implement your thread safety policies yourself as usual.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.