I am trying to build a network related program in Java. I have previous experience with C. In C, when you run thread, you define which method you want it to be run as a thread.
However, in Java, it seems that thread always runs with method run() and there can be 1 method with that name in each class.
I want to have at least 2 threads, one thread for working on calculations, and one thread to work on communications with other applications. (Even if this can be done with 1 thread, I just want to know what would be a correct way to run 2 threads that does totally different jobs)
Below is just a sample code how I implemented the thread. If thread generated by below codes does communication, what would be a nice way to create another thread that does calculation?
public class Server implements Runnable{
static Thread myThread;
public void run() {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
myThread = new Thread(new Server());
}
}
Don’t put a
mainmethod in the class that implementsThreadorRunnable. You could implement what you want with your currentServerimplementation, but I don’t see a good reason to do so. Separate out the concerns, and KISS:public static void main(String[] args)methodServer implements Runnableclass (one type of thread)Calculations implements Runnableclass (the other type of thread)The class with the
mainmethod would start theServerandCalculationsthreads.