2016-03-26 47 views
1

我试图启动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个核心。

+1

你还没有开始udp!添加udp.start(); –

回答

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

}

如果UdpServerTcpServer已经延伸Thread或实现Runnable调用它,你可以通过start()方法直接启动它们,而不用创建包装类。

2

您只能创建udp线程,但不能真正启动它。加上:

udp.start(); 

其次,你直接从构造函数,而不是run方法启动循环。更改为:

public static class TcpServerStarter implements Runnable { 
    public void run(){ 
     new TcpServer(8500).run(); 
    } 
} 

对于UdpServerStarter也是类似的。

+0

试过了。没有。 – Lundira

+0

@Lundira:还有一个问题,请参阅更新的答案。 –

2

当您致电new TcpServer(8500).run();在TcpServerStarter的构造

它开始在主线程中运行TCPSERVER。 我猜测它只是永远服务,这就是为什么它没有达到以下任何代码。

还什么兹比涅克说:你没有启动UDP线程。

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

}

1

线程对象必须启动使得相应Thread的执行可以开始。

我在你的代码中看到下面的代码

System.out.print("Reach 1"); 
Thread tcp = new Thread(new `TcpServerStarter`()); 

到现在为止你还没有叫tcp.start()。截至目前已创建的Thread.java一个新的对象,它是确定的,但主要的问题我觉得是的TcpServerStarter.java构造函数的代码写它,它阻止你前进的main method执行流程。

请参阅TcpServerStarter.java class是线程本身,因为它延伸Thread.java。应该override run method在您的TcpServerStarter.java您应该将代码从构造函数移动到run()方法。而不是创建`线程的对象。java如下修改代码。

Thread tcp = new `TcpServerStarter`(); 

然后你需要调用你创建的Thread对象的start。

tcp.start() 

同样,您需要修改代码,包括使用UdpServerStarter

相关问题