//可以启动一个线程来做时间计算//也可以用timer来实现定时 timer使用wait 我这里用sleep//关键不是用什么而是用线程来解决问题import java.util.Scanner;public class Test {public static void main(String[] args) {Runnable r = new Runnable() {final long start = System.currentTimeMillis() ;public void run() {try {while(true) {Thread.sleep(1000);long now = System.currentTimeMillis();if(now-start>=10000) {System.out.println("打字时间结束了");System.exit(0);}}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};System.out.println("可以开始打字了,10秒后就不能打了");Scanner s = new Scanner(System.in);Thread t = new Thread(r);t.start();while(s.hasNext()) {System.out.println(s.nextLine());}}}
取消
评论