-
My thread will not start
I have my thread class (which transmits a datagram):
Code:
public class heartbeat extends Thread {
private int portNum;
private InetAddress address;
private int siteNum;
public heartbeat(InetAddress pAddress, int pPortNum, int pSiteNum)
{
portNum = 3001;
address = pAddress;
siteNum = pSiteNum;
}
public void Run()
{
byte[] buf = new byte[256];
String stringToSend = "[H," + siteNum + "]";
buf = stringToSend.getBytes();
System.out.println("Port is " + portNum);
try {
DatagramSocket socket = new DatagramSocket(portNum);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, portNum);
while(true)
{
System.out.println(stringToSend);
socket.send(packet);
}
} catch (Exception e)
{ System.exit(0); }
}
}
and then the following two lines in my main program.
Code:
heartbeat myHeartbeat = new heartbeat(serverAddr, serverPort, siteID);
myHeartbeat.start();
It all looks right to me but the thread will not run and I can't see whats wrong with it!! Please help.
-
Capitalization.
public void run() overrides the run method
public void Run() makes a new method that doesn't override it.
-
change your Run into run() instead
-
In the future use the @Override statement before methods that are overrides. The compiler will then tell you if your method is NOT overriding anything.
-
Change public void Run()
to
public void run()