我正在尝试启动2个线程,一个用于tcp,一个用于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();
}
}
}
仅打印“达到1”。我读到如果我有单核心可能会发生这种情况,但我有2个核心。
答案 0 :(得分:2)
你只创建udp线程但不是真的启动它。加上这个:
udp.start();
其次,您直接从构造函数而不是run方法启动循环。改为:
public static class TcpServerStarter implements Runnable {
public void run(){
new TcpServer(8500).run();
}
}
同样适用于UdpServerStarter。
答案 1 :(得分:2)
致电new TcpServer(8500).run()
时;在TcpServerStarter的构造函数
它开始在MAIN线程中运行tcpServer。 我猜它只是永远服务,这就是为什么它没有达到以下任何代码。
Zbynek也说了什么:你没有启动udp线程。
答案 2 :(得分:1)
将代码放入名为run()
的方法中,而不是从构造函数中调用它(调用run()
将阻止执行)
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();
}
}
}
如果UdpServer
和TcpServer
已经扩展Thread
或实现Runnable
,您可以通过start()
方法直接启动它们,而无需创建包装器类。
答案 3 :(得分:1)
不改变你的代码,这个可行。我不确定TcpServer或UdpServer的具体实现是什么样的。所以现在实现了一些假的。我看到所有打印输出都按预期打印。
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();
}
}
}
答案 4 :(得分:1)
必须启动线程对象,以便可以启动相应Thread
的执行。
我在代码中看到代码
System.out.print("Reach 1");
Thread tcp = new Thread(new `TcpServerStarter`());
直到现在你尚未调用tcp.start()
。直到现在你已经创建了一个Thread.java
的新对象,这是好的,但我觉得主要的问题是TcpServerStarter.java
的构造函数编写的代码阻止了main method
的执行流程进取。
TcpServerStarter.java
类是一个线程,因为它扩展了Thread.java
。应该override run method
TcpServerStarter.java
你应该将代码从构造函数移动到run()方法。而不是创建`Thread.java的对象,而是修改下面的代码。
Thread tcp = new `TcpServerStarter`();
然后你需要在你创建的Thread对象上调用start。
tcp.start()
同样,您需要修改代码,包括UdpServerStarter
的用法。