日期:2014-05-20 浏览次数:20880 次
package net; public class NetThread implements Runnable{ String data = null; //constructor public NetThread(String data){ this.data = data; } //run method public void run(){ try { System.out.println("Start sending..."); Thread.sleep(10000); //delay time System.out.println("Sending completely! The message is: " + data); } catch (InterruptedException e) { e.printStackTrace(); } } }
package net; import java.io.*; public class Test { public static void main(String[] args){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = null; while(true){ try { System.out.println("Please enter: "); input = br.readLine(); //accept input from console if(input.equals("quit")){ System.out.println("Quit successfully!"); break; // exit } //create and start sending thread Thread nt = new Thread(new NetThread(input)); nt.start(); } catch (IOException e) { e.printStackTrace(); }finally{ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
public static void main(String[] args){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = null; while(true){ try { System.out.println("Please enter: "); input = br.readLine(); //accept input from console if(input.equals("quit")){ System.out.println("Quit successfully!"); br.close(); break; // exit } //create and start sending thread Thread nt = new Thread(new NetThread(input)); nt.start(); } catch (IOException e) { e.printStackTrace(); } } }
------解决方案--------------------
public static void main(String[] args){ while(true){ //放这试下吧。 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = null; try { System.out.println("Please enter: "); input = br.readLine(); //accept input from console if(input.equals("quit")){ System.out.println("Quit successfully!"); break; // exit } //create and start sending thread Thread nt = new Thread(new NetThread(input)); nt.start(); } catch (IOException e) { e.printStackTrace(); }finally{ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } }