When i boot this, TCPServer starts gets running but after that UDPServer does not run? And 开发者_如何学运维if i swap those always the first one runs. How can i boot both?
All the rest boots fine. How can i solve this?
/**
* Boot baby boot
* @param args
*/
public static void main(String[] args)
{
/* Heavy load - 1 */
new Thread(new Runnable()
{
public void run()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
TCPServer tcpserver = null;
try {
tcpserver = new TCPServer(8888);
}
catch (IOException e){
e.printStackTrace(System.err);
}
tcpserver.waitForConnections();
}
});
}
}).start();
new Thread(new Runnable()
{
public void run()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
UDPServer udpserver = null;
try {
udpserver = new UDPServer(8889);
}
catch (IOException e){
e.printStackTrace(System.err);
}
udpserver.waitForConnections();
}
});
}
}).start();
/* Heavy load - 2 */
try {
Game3Dstart();
} catch (Exception ex) {
}
/* Finally */
j = new main();
j.setVisible(true);
}
It's because waitForConnections
is blocking. You need to launch them async, in separate threads.
new Thread(new Runnable(){
public void run(){
TCPServer tcpserver = null;
try {
tcpserver = new TCPServer(8888);
}
catch (IOException e){
e.printStackTrace(System.err);
}
tcpserver.waitForConnections();
}
}).start();
new Thread(new Runnable(){
public void run(){
UDPServer udpserver = null;
try {
udpserver = new UDPServer(8889);
}
catch (IOException e){
e.printStackTrace(System.err);
}
udpserver.waitForConnections();
}
}).start();
Put both UDP and TCP servers on different threads:
/* Heavy load - 1 */
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
TCPServer tcpserver = null;
try {
tcpserver = new TCPServer(8888);
}
catch (IOException e){
e.printStackTrace(System.err);
}
tcpserver.waitForConnections();
catch (IOException e){
e.printStackTrace(System.err);
}
}
}
/* Heavy load - 1 */
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
UDPServer udpserver = null;
try {
udpserver = new UDPServer(8889);
}
catch (IOException e){
e.printStackTrace(System.err);
}
udpserver.waitForConnections();
}
});
);
精彩评论