user1972748
user1972748

Reputation: 69

Multiples simultaneous accesses to a single thread

I'm currently building a multi-threaded server / client in Java with UDP via datagram sockets/packets. I'm having a hard time understanding the proper use of threads and would like some clarification. I'll first give an example of what I'm doing.

Thread a;
Thread b(a);

a.start
b.start

//simple enough, now inside b imagine this,
Thread c(a);
if (case)
{
    c.start //therefore I can have a lot of thread c's running at once, 
}

//now inside c imagine this
if (case)
{
    a.somefunction();
}

Ultimately my question is very hard to ask, but is the above sudo appropriate use of threads? Even though there is only 1 thread a running at a time, it may be accessed from multiple other places concurrently. Will this cause issues?

Thanks for any responses.

-William

Just adding an edit for further clarification.

Thread a would be my packet sender, it sends packets from the server to the client. Thread b would be my packet listener, it receives packets from the clients, and sends them off to thread C, the packet parser. (so I can parse multiple packets at the same time). Thread c, the packet parser, may need to send a response back to the client, so it would invoke a function in a which queues a packet up to be sent off.

Thanks again,

Edit again,

sample thread using functions

package server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Vector;

public class ServerSenderThread extends Thread
{
    DatagramSocket serverSocket;
    Vector<DatagramPacket> outGoingPackets = new Vector<DatagramPacket>();

    public ServerSenderThread(DatagramSocket serverSocket)
    {
        this.serverSocket = serverSocket;
    }

    public void run()
    {
        while (true)
        {
            if (outGoingPackets.size() == 0)
            {
                try
                {
                    Thread.sleep(50);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            else
            {
                try
                {
                    send();
                }
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public void addSend(DatagramPacket packet)
    {
        outGoingPackets.addElement(packet);
    }

    public void send() throws IOException
    {
        DatagramPacket packet = outGoingPackets.get(0);
        outGoingPackets.removeElementAt(0);

        InetAddress address = packet.getAddress();
        int port = packet.getPort();
        byte[] buf = new byte[256];
        String dString = "Data Only the Server Knows";
        buf = dString.getBytes();
        packet = new DatagramPacket(buf, buf.length, address, port);

        System.out.println("Sserver sending packet");   
        serverSocket.send(packet);

    }

}

Upvotes: 0

Views: 209

Answers (3)

Pedro R.
Pedro R.

Reputation: 142

I think you can have issues if a has one socket (or another resource) and every call from every thread uses that resource. If every call uses a new (or diferent) socket I guess there wouldn't be problems.

One way to handle this is synchronizing accesses:

if (case)
{
    synchronized (a) 
    {
        a.somefunction();
    {
}

or better add synchronized in the somefunction definition

public void synchronized somefunction() {
    ...
}

Another way would be changing the solution design, with producer-consumer pattern, with nobody accessing directly to a, but adding the packets to send to a list that would be monitorized by c (the consumer) and would send every package that would appear on the list. The list would be synchronized, but the synchronization would be less intrusive because it would affect only to adding the element to the list and no to all the processing of the element.

Update: I also recommend you the book Java Concurrency In Practice, pretty straight forward reading, and even this review from the blog The Java Specialists, that was my source for the book.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533530

The sending and receiving of packet is generally simple unless you have a high rate e.g. 10+K/second. The processing of these packets could take some time but unless this is really expensive (much more than parsing) I would consider using one thread for all these functions. It will simplify the code and make debugging it easier. i.e. I would make the design as simple as possible unless you know you need to make it more complicated.

If you compare the single threaded version of what you have above, you can see it's much simpler which is a clear benefit whereas using multiple threads in this situation is not a clear benefit.

public class DataPacket {
    final DatagramSocket serverSocket;

    public DataPacket(InetAddress address, int port) throws SocketException {
        this.serverSocket = new DatagramSocket(port, address);
    }

    public void send(String message) throws IOException {
        byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
        serverSocket.send(new DatagramPacket(bytes, bytes.length));
        System.out.println("Sent " + message);
    }
}

Upvotes: 1

Alex DiCarlo
Alex DiCarlo

Reputation: 4891

For multithreaded applications similar to what you described, it's best to use a BlockingQueue for message passing between threads. This will be automatically thread-safe, and hands off messages exactly as you describe with put(message) and take().

For example, your packet listener thread could have a BlockingQueue that it puts messages on that the packet parser takes from.

Upvotes: 0

Related Questions