62,612
社区成员
发帖
与我相关
我的任务
分享
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){
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();
}
}
}
}
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();
}
}
}
finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
import java.io.*;
public class Test1 {
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();
}
}
//把这句放到循环外面去 你再运行!
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}