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);
}
}
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.