一个线程的问题
代码如下:
import java.io.IOException;
import java.net.*;
public class TestMonitorPort {
public static void main(String[] args) {
printReceiveInfomationFromPort(6000);
}
static void printReceiveInfomationFromPort(int port) {
new Thread(new MonitorPortRunnable(port)).start();
}
}
class MonitorPortRunnable implements Runnable {
byte buf[] = new byte[1024];
DatagramSocket ds = null;
DatagramPacket dp = null;
int localReceivePort ;
public MonitorPortRunnable(int localReceivePort) {
this.localReceivePort = localReceivePort;
}
public void run() {
dp = new DatagramPacket(buf, 0, 1024);
try {
ds = new DatagramSocket(localReceivePort);
} catch (SocketException e1) {
//prompt("本地接收端口已被使用");
System.exit(0);
}
while (true) {
try {
ds.receive(dp);
System.out.println("信息来自:" + this.localReceivePort);
} catch (IOException e) {
ds.close();
e.printStackTrace();
}
String receiveMessage = new String(dp.getData(), 0, dp
.getLength());
System.out.println(receiveMessage);//暂时打印到控制台,一般输出到文件
}
}
本来运行一切正常,但是导出俩次jar包后,再次运行,每次都是一闪就过去了
而不是正常情况的一直监听6000端口..
是什么问题呢?
import java.io.IOException;
import java.net.*;
public class TestMonitorPort {
public static void main(String[] args) {
printReceiveInfomationFromPort(6000);
}
static void printReceiveInfomationFromPort(int port) {
new Thread(new MonitorPortRunnable(port)).start();
}
}
class MonitorPortRunnable implements Runnable {
byte buf[] = new byte[1024];
DatagramSocket ds = null;
DatagramPacket dp = null;
int localReceivePort ;
public MonitorPortRunnable(int localReceivePort) {
this.localReceivePort = localReceivePort;
}
public void run() {
dp = new DatagramPacket(buf, 0, 1024);
try {
ds = new DatagramSocket(localReceivePort);
} catch (SocketException e1) {
//prompt("本地接收端口已被使用");
System.exit(0);
}
while (true) {
try {
ds.receive(dp);
System.out.println("信息来自:" + this.localReceivePort);
} catch (IOException e) {
ds.close();
e.printStackTrace();
}
String receiveMessage = new String(dp.getData(), 0, dp
.getLength());
System.out.println(receiveMessage);//暂时打印到控制台,一般输出到文件
}
}
本来运行一切正常,但是导出俩次jar包后,再次运行,每次都是一闪就过去了
而不是正常情况的一直监听6000端口..
是什么问题呢?
...全文
相关推荐
实现多线程同步问题--一个线程自加一个线程显示
面试的时候碰到的一个问题,实现多线程对一个全局数的自加,另外一个线程当全局数自加1以后立马显示这个数的现值。
项目开发中碰到的一个线程问题 (二)
NULL 博文链接:https://alanlg.iteye.com/blog/1697445
一个多线程示例程序及多线程常见问题介绍
一个多线程示例程序及多线程常见问题介绍。实例程序,带有源码,直接可用,c++builder的精彩编程!!
c++ 多线程向同一个文本写入信息 解决同步与互斥问题
c++ 多线程向同一个文本写入信息 解决同步与互斥问题
一个高频问题:异步操作会创建线程吗?.doc
一个高频问题:异步操作会创建线程吗?.doc


Java SE
