Reputation: 179
I am trying to start 2 threads, one for tcp and one for udp
package com.main;
import com.utility.HibernateUtil;
public class ServerStarter {
public static void main(String[] args) {
System.out.print("Reach 1");
Thread tcp = new Thread(new TcpServerStarter());
tcp.start();
System.out.print("Reach 2");
Thread udp = new Thread(new UdpServerStarter());
System.out.print("Reach 3");
HibernateUtil.buildSessionFactory();
System.out.print("Reach 4");
}
public static class TcpServerStarter extends Thread{
public TcpServerStarter(){
new TcpServer(8500).run();
}
}
public static class UdpServerStarter extends Thread{
public UdpServerStarter(){
new UdpServer(1000).run();
}
}
}
Only "reach 1" is printed. I read that this may occur if i have single core, however i have 2 cores.
Upvotes: 1
Views: 137
Reputation: 5316
A thread object must be started so that the execution of the corresponding Thread
can be initiated.
I see in your code below code
System.out.print("Reach 1");
Thread tcp = new Thread(new `TcpServerStarter`());
Till now you have not yet called the tcp.start()
. Till now you have created a new object of Thread.java
which is OK but the main problem I feel is with the constructor of TcpServerStarter.java
the code written it it prevents the flow of execution of your main method
ahead.
See TcpServerStarter.java
class is a Thread itself as it extends Thread.java
. should override run method
in your TcpServerStarter.java
You Should move the code from the constructor to the run() method. Instead of creating the objects of `Thread.java modify the code as below.
Thread tcp = new `TcpServerStarter`();
Then you need to call start on the Thread object you have created.
tcp.start()
Similarly you need to modify the code including the usage of UdpServerStarter
.
Upvotes: 1
Reputation: 1172
Without changing too much your code, this one works. I am not sure what is your specific implementation fo TcpServer or UdpServer looks like. So implemented some dummy ones for now. I see that all the print outs print as expected.
package network;
class TcpServer implements Runnable{
TcpServer(int port){
}
@Override
public void run() {
for (int i=0; i < 10; i++){
System.out.println("Thread printing:" + i + "tid:" + Thread.currentThread().getId());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class UdpServer implements Runnable{
UdpServer(int port){
}
@Override
public void run() {
for (int i=0; i < 10; i++){
System.out.println("Thread printing:" + i + "tid:" + Thread.currentThread().getId());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ServerStarter {
public static void main(String[] args) {
System.out.print("Reach 1");
Thread tcp = new Thread(new TcpServerStarter());
tcp.start();
System.out.print("Reach 2");
Thread udp = new Thread(new UdpServerStarter());
System.out.print("Reach 3");
//HibernateUtil.buildSessionFactory();
System.out.print("Reach 4");
}
public static class TcpServerStarter extends Thread{
public TcpServerStarter(){
new TcpServer(8500).run();
}
}
public static class UdpServerStarter extends Thread{
public UdpServerStarter(){
new UdpServer(1000).run();
}
}
}
Upvotes: 1
Reputation: 18825
You only create udp thread but not really start it. Add this:
udp.start();
Secondly you start the loop directly from the constructor instead of run method. Change to:
public static class TcpServerStarter implements Runnable {
public void run(){
new TcpServer(8500).run();
}
}
And similarly for the UdpServerStarter.
Upvotes: 2
Reputation: 124
When you call new TcpServer(8500).run()
; in TcpServerStarter's constructor
It starts running the tcpServer in the MAIN thread. I'm guessing that it just serves forever and this is why it doesn't reach any of the following code.
Also what Zbynek said : you didn't start the udp thread.
Upvotes: 2
Reputation: 12837
Put the code into method called run()
instead of calling it from the constructor (calling the run()
will block the execution)
public static class TcpServerStarter extends Thread {
@Override
public void run() {
new TcpServer(8500).run();
}
}
public static class UdpServerStarter extends Thread {
@Override
public void run() {
new UdpServer(1000).run();
}
}
}
If the UdpServer
and TcpServer
already extends Thread
or implements Runnable
, you can start them directly by the start()
method w/o creating the wrapper classes.
Upvotes: 1