多线程遇到的问题

开始的程序,执行后发现有时输入bye后,程序会一直等待,不退出

package net; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; //import java.text.DateFormat; //import java.text.SimpleDateFormat; //import java.util.Date; public class UdpChat { DatagramSocket ds; String ipString; String portString; boolean bChating = true;//the flag to stop 2010-2-24 public UdpChat() { } public UdpChat(String ip, String port) { try { ipString = ip; portString = port; ds=new DatagramSocket(Integer.parseInt(port)); } catch(Exception ex) { ex.printStackTrace(); } } public void recv() { new Thread(new Runnable() { public void run() { byte buf[]=new byte[1024]; DatagramPacket dp= new DatagramPacket(buf,1024); while(bChating) { try { ds.receive(dp); // lst.add(new String(buf,0,dp.getLength())+ // ":from"+dp.getAddress().getHostAddress(),0); String strRecv=new String(dp.getData(),0,dp.getLength()) + " from " + dp.getAddress().getHostAddress()+":"+dp.getPort(); System.out.println(strRecv); } catch(Exception e) { if (!ds.isClosed()) { e.printStackTrace(); } } } } }).start(); } public void send() { // String ipString = ip; new Thread(new Runnable() { public void run() { while(true) { try { // DatagramSocket ds=new DatagramSocket(); // String str="hello world"; String str = getString(); DatagramPacket dp=new DatagramPacket(str.getBytes(),str.getBytes().length, InetAddress.getByName(ipString),Integer.parseInt(portString)); ds.send(dp); if (str.equalsIgnoreCase("bye") || str.equalsIgnoreCase("quit")) { bChating = false; return; } // System.out.println("send"); } catch (Exception ex) { ex.printStackTrace(); } } } }).start(); } //接收和发送封装在一起 public void talk() { recv(); send(); } public static void main(String [] args) { if (args.length != 2) { System.out.println("输入ip及端口"); return; } UdpChat chat = new UdpChat(args[0], args[1]); // chat.recv(); // chat.send(); chat.talk(); } public static String getString() { String s = null; try { InputStreamReader ir; BufferedReader inBufferedReader; //从键盘接收了一个字符串的输入,并创建了一个字符输入流的对象 ir = new InputStreamReader(System.in); //从输入流中读入一行,并将读取的值赋值给字符串变量s inBufferedReader = new BufferedReader(ir); s = inBufferedReader.readLine(); // System.out.println("Input value is: "+s); } catch (IOException e) { System.out.println(e); } return s; } } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/aaajj/archive/2010/02/08/5299176.aspx

 

发现是线程切换造成的

当按照1-2-3-4 的顺序切换时,出现问题

 

修改后的代码

package net; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; //import java.text.DateFormat; //import java.text.SimpleDateFormat; //import java.util.Date; public class UdpChat { DatagramSocket ds; String ipString; String portString; boolean bChating = true;//the flag to stop 2010-2-24 public UdpChat() { } public UdpChat(String ip, String port) { try { ipString = ip; portString = port; ds=new DatagramSocket(Integer.parseInt(port)); } catch(Exception ex) { ex.printStackTrace(); } } public void recv() { new Thread(new Runnable() { public void run() { byte buf[]=new byte[1024]; DatagramPacket dp= new DatagramPacket(buf,1024); //3 while(bChating) { //test // if (bChating) { // System.out.println("true"); // } // else { // System.out.println("false"); // } try { //2 ds.receive(dp); // lst.add(new String(buf,0,dp.getLength())+ // ":from"+dp.getAddress().getHostAddress(),0); String strDataString = new String(dp.getData(),0,dp.getLength()); String strRecv = strDataString + " from " + dp.getAddress().getHostAddress()+":"+dp.getPort(); System.out.println(strRecv); //1 2 3 4 的顺序会使程序等待,这样来处理, //或者把1 4捆绑起来执行,同步代码块,进行原子操作 synchronized if (strDataString.equalsIgnoreCase("bye") || strDataString.equalsIgnoreCase("quit")) { bChating = false; return; } } catch(Exception e) { if (!ds.isClosed()) { e.printStackTrace(); } } } } }).start(); } public void send() { // String ipString = ip; new Thread(new Runnable() { public void run() { while(true) { try { // DatagramSocket ds=new DatagramSocket(); // String str="hello world"; String str = getString(); DatagramPacket dp=new DatagramPacket(str.getBytes(),str.getBytes().length, InetAddress.getByName(ipString),Integer.parseInt(portString)); //1 ds.send(dp); //4 if (str.equalsIgnoreCase("bye") || str.equalsIgnoreCase("quit")) { bChating = false; return; } // System.out.println("send"); } catch (Exception ex) { ex.printStackTrace(); } } } }).start(); } //接收和发送封装在一起 public void talk() { recv(); send(); return; } public static void main(String [] args) { if (args.length != 2) { System.out.println("输入ip及端口"); return; } UdpChat chat = new UdpChat(args[0], args[1]); // chat.recv(); // chat.send(); chat.talk(); return; } public static String getString() { String s = null; try { InputStreamReader ir; BufferedReader inBufferedReader; //从键盘接收了一个字符串的输入,并创建了一个字符输入流的对象 ir = new InputStreamReader(System.in); //从输入流中读入一行,并将读取的值赋值给字符串变量s inBufferedReader = new BufferedReader(ir); s = inBufferedReader.readLine(); // System.out.println("Input value is: "+s); } catch (IOException e) { System.out.println(e); } return s; } }

你可能感兴趣的:(多线程遇到的问题)