我模拟一个:public class Test2 {
static  String a;
public static void main(String[] args) throws InterruptedException {
T t = new T();
t.setDaemon(true);
t.start();
System.out.print("等待用户输入:");
Thread.sleep(3000);
if(a==null){
System.out.println("no");
}else{
System.out.println(a);
}
System.out.println("over");
}}
class T extends Thread{
@Override
public void run() {
Test2.a = new Scanner(System.in).nextLine();
}
}

解决方案 »

  1.   

    很简单的代码就可以做出来public String parseMagcard(Charset charset) {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    long startTime = System.currentTimeMillis();
    String line = null;
    try {
    while ((System.currentTimeMillis() - startTime) < time * 1000
    && !in.ready()) {
    }
    if (in.ready()) { line = in.readLine();
    } else {
    // System.out.println("You did not enter data");
    line = "-1";
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    return line; }