java线程中监听值的变化? socket发送后,接收线程用while(1){get = br.readline(); if(get!=null){recvdata = get;}}在主程序中写入流(发送),怎样在接受线程接到数据时立即再主程序中处理? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 就是一个Frame或者一个class里面启动线程,然后要监控线程里的变量变化public class Frame1 extends JFrame { // 建立连接 try { connection = new Socket(InetAddress.getLocalHost(),8021); JOptionPane.showMessageDialog(this,"连接成功"); }catch(Exception es) { JOptionPane.showMessageDialog(this,"错误2"+es.toString()); } // 发送 try { // io流 is = connection.getInputStream(); os = connection.getOutputStream(); br = new BufferedReader(new InputStreamReader(is)); pw = new PrintWriter(os); // 线程 t1 = new Thread(new receiver()); t1.start(); } catch (Exception es) { JOptionPane.showMessageDialog(this, "错误" + es.toString()); } class receiver implements Runnable { public receiver() { } public void run() { while(true) { try { changableValue = br.readLine(); if(changableValue != null) { recvdata = changableValue ; } else { } }catch(Exception ex) { //JOptionPane.showMessageDialog(null, "错误1" + ex.toString()); } } } }} Swing的话,将其它线程里得到的数据,及处理过程,封装成一个 Runnable 任务,提交给 Swing线程执行1。SwingUtilities.invokeAndWait(Runnable r)阻塞执行2。SwingUtilities.invokeLater(Runnable r)异步执行changableValue = br.readLine();if(changableValue==null) break;else{ final String v = changableValue; SwingUtilities.invokeLater(new Runnable(){ public void run(){ //将v显示到Swing界面组件上 } });} if(changableValue != null) { recvdata = changableValue ; } else { } 上面回复里后面一段代码忘了删,无视之CSDN 怎么不能编辑自己的回帖,真要命 敢不敢有人留个言???T 。T 菜鸟啊啊!!我是。。。各位大神。。。帮帮忙好吧。。我真的不会 请帮我分析算法. 什么是对象串行化 是干什么的? string的中文问题 问题六:final修饰符的用法? System.out.flush();到底有什么用? 如何注意大小写问题 redhat 运行java程序 那位能提供java applet实现的目录树(内容来自数据库)的源码? ant的问题 大侠们,如何用jS判断本地某个文件是否存在? 请假熬一个问题先 各位大虾帮帮忙
就是一个Frame或者一个class里面启动线程,然后要监控线程里的变量变化
public class Frame1 extends JFrame {
// 建立连接
try
{
connection = new Socket(InetAddress.getLocalHost(),8021);
JOptionPane.showMessageDialog(this,"连接成功");
}catch(Exception es)
{
JOptionPane.showMessageDialog(this,"错误2"+es.toString());
}
// 发送
try
{
// io流
is = connection.getInputStream();
os = connection.getOutputStream();
br = new BufferedReader(new InputStreamReader(is));
pw = new PrintWriter(os);
// 线程
t1 = new Thread(new receiver());
t1.start();
} catch (Exception es) {
JOptionPane.showMessageDialog(this, "错误" + es.toString());
}
class receiver implements Runnable
{
public receiver()
{
}
public void run()
{
while(true)
{
try
{
changableValue = br.readLine();
if(changableValue != null)
{
recvdata = changableValue ;
}
else
{
}
}catch(Exception ex)
{
//JOptionPane.showMessageDialog(null, "错误1" + ex.toString());
}
}
} }
}
1。SwingUtilities.invokeAndWait(Runnable r)阻塞执行
2。SwingUtilities.invokeLater(Runnable r)异步执行changableValue = br.readLine();
if(changableValue==null)
break;
else{
final String v = changableValue;
SwingUtilities.invokeLater(new Runnable(){
public void run(){
//将v显示到Swing界面组件上
}
});
} if(changableValue != null)
{
recvdata = changableValue ;
}
else
{
}