如何让程序在一定时间后在没有输入值的情况下采用默认值接着运行啊?? 各位兄弟姐妹们,帮帮忙啊,我想让Java程序在3秒后有输入值的话就采用输入值运行;没有输入值的情况下,采用默认值接着运行该怎么做啊?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;public class Test{ public String parameter = null; private boolean finish = false; public void test() { if(finish) { return; } if(parameter == null) { System.out.println("Do without parameter"); } else { System.out.println("Do with parameter:" + parameter); } finish = true; System.exit(0); } public void load() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); new Thread() { public void run() { try { Thread.sleep(3000); } catch(Exception ex) { ex.printStackTrace(); } test(); } }.start(); parameter = br.readLine(); test(); } public static void main(String[] args) throws Exception { Test t = new Test(); t.load(); }} 我在想这句话: parameter = br.readLine();//估计会一直等着输入吧? 1#的方法是使用了System.exit(0),造成假象而已,如果还想进行其它的动作,好像还是不行的,比如说还有其它的输入的话...... 也可以考虑用一个线程读取输入,并放入ArrayList中 试了一下,停不了线程~~~~import java.io.*;public class Test{ public String parameter = null; private boolean finish = false; Thread input=null; public Test() { //启动输入线程 final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); input=new Thread(){ public void run() { try { parameter = br.readLine(); finish=true; } catch(Exception ex) { ex.printStackTrace(); } } }; input.start(); //启动终止线程 new Thread(){ public void run() { try { Thread.sleep(3000); } catch(Exception ex) { ex.printStackTrace(); } try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } test(); } }.start(); } public void test() { if(finish) { return; } if(parameter == null) { //终止输入线程 if(input!=null) { System.out.println("中断"); input.interrupt();//中断线程 } } else { finish = true; return; //System.out.println("Do with parameter:" + parameter); } } public static void main(String[] args) throws Exception { Test t = new Test(); }} 这个正点,我本身也通过多线程实现了这个过程,但就是卡在了这里,用Scanner对象也是一样的 wd9053(该用户很聪明,没有设置昵称) ========》》名副其实,牛,夸个 O(∩_∩)O我怎么就忘了system.exit(0)呢,还有两处调用test()方法很妙,哈哈 同时也很感谢AWUSOFT的帮助 你使用了System.exit(0),就是相当于程序处理完了? 这个就是唯一的缺陷了,流引起的堵塞会一直存在,直到最后System.exit(0),如果可以,我当然还是希望能在程序接着运行的开始阶段就解决掉这个问题,而不是等到最后在解决掉。这个也就是我还没有结贴的原因,不知道还有没高手解决这个问题,期待中...... 用Robot类模拟键盘输入回车import java.awt.Robot;import java.awt.event.KeyEvent;import java.io.*;public class Test{ public String parameter = null; private boolean finish = false; public void test() { if(finish) { return; } if(parameter == null) { System.out.println("Do without parameter"); } else { System.out.println("Do with parameter:" + parameter); } finish = true; //System.exit(0); end(); } public void load() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); new Thread() { public void run() { try { Thread.sleep(3000); } catch(Exception ex) { ex.printStackTrace(); } test(); } }.start(); parameter = br.readLine(); test(); } public void end() { try { Robot t = new Robot(); t.keyPress(KeyEvent.VK_ENTER); } catch(Exception ex) { ex.printStackTrace(); } } public static void main(String[] args) throws Exception { Test t = new Test(); t.load(); }} Robot类方式,我测试了下,好像还是不行啊,当没有输入值时,流一直都处于堵塞状态,都不会结束了;当有输入值时,流的堵塞现象都要等到3秒以后才能结束啊;不过对于这个问题,你第一次的回答我还是挺满意的,虽然有一点缺陷,呵呵。谢谢了 用BufferedReaderl里ready()的方法判断一下. 稍微修改了下,在我的电脑上正常1、没有输入时,程序会在3秒后向cmd窗口中输入一个回车,程序运行后就别动了,要不t.keyPress()时cmd窗口没有获得焦点,程序不会结束2、你感觉有延迟可能是因为创建Robot和执行操作需要时间import java.awt.Robot;import java.awt.event.KeyEvent;import java.io.*;public class Test{ public String parameter = null; private boolean finish = false; Robot t; public Test() { try { t = new Robot(); } catch(Exception ex) { ex.printStackTrace(); } } public void test() { if(finish) { return; } finish = true; if(parameter == null) { System.out.println("Do without parameter"); } else { System.out.println("Do with parameter:" + parameter); } } public void load() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); new Thread() { public void run() { try { for(int i = 0; i < 3000; i++) { if(finish) { return; } Thread.sleep(1); } } catch(Exception ex) { ex.printStackTrace(); } test(); end(); } }.start(); parameter = br.readLine(); test(); end(); } public void end() { t.keyPress(KeyEvent.VK_ENTER); } public static void main(String[] args) throws Exception { Test t = new Test(); t.load(); }} public class Console { private static class InputThread extends Thread { private String value; private boolean cancelled; private boolean got; public void run() { try { String line = null; StringBuffer b = new StringBuffer(); while (!cancelled) { if (System.in.available() > 0) { char[] cache = new char[System.in.available()]; int c = new InputStreamReader(System.in).read(cache); this.got = true; this.value = new String(cache); break; } else { Thread.yield(); continue; } } } catch (IOException e) { e.printStackTrace(); this.cancelled = true; } finally { /* don't close System.in */ } } public String getValue() { return this.got ? this.value : null; } public void cancel() { this.cancelled = true; } } public static void main(String[] args) { InputThread helper = new InputThread(); System.out.println("Please enter value (time out is 10 seconds):"); helper.start(); try { helper.join(10 * 1000); helper.cancel(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } String value = helper.getValue(); if (value == null) { System.out.println("Timed out, use default value instead"); } else { System.out.println("Entered :" + value); } }} 非常感谢wd9053、humanity,你们的方法都挺好的,to wd9053: 我测试时出现问题,是由于我失误,呵呵to humanity:available()方法就解决问题了,看来我对API还是不熟分不是很多,还请谅解,呵呵 分享:一道BT试题 java swing单击按钮隐藏显示JtextField的问题? JAVA怎么获取他安装的路径 使用FlowLayout如何使组件换行显示 连接数据库SQL的错误~ 如何将java程序打包成exe文件? 程序出错...大家帮我看看 请问ArrayList和Vector哪个好? 初学者:为什么单独声明类建立object? java 线程画图问题 写一些程序是不是要装j2ee? Jxta p2p 通讯问题 急寻答案
import java.io.*;public class Test
{
public String parameter = null;
private boolean finish = false;
public void test()
{
if(finish)
{
return;
}
if(parameter == null)
{
System.out.println("Do without parameter");
}
else
{
System.out.println("Do with parameter:" + parameter);
}
finish = true;
System.exit(0);
}
public void load() throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
new Thread()
{
public void run()
{
try
{
Thread.sleep(3000);
}
catch(Exception ex)
{
ex.printStackTrace();
}
test();
}
}.start();
parameter = br.readLine();
test();
}
public static void main(String[] args) throws Exception
{
Test t = new Test();
t.load();
}
}
试了一下,停不了线程~~~~
import java.io.*;public class Test
{
public String parameter = null;
private boolean finish = false;
Thread input=null;
public Test()
{
//启动输入线程
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input=new Thread(){
public void run()
{
try
{
parameter = br.readLine();
finish=true;
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
input.start();
//启动终止线程
new Thread(){
public void run()
{
try
{
Thread.sleep(3000);
}
catch(Exception ex)
{
ex.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
test();
}
}.start();
}
public void test()
{
if(finish)
{
return;
}
if(parameter == null)
{
//终止输入线程
if(input!=null)
{
System.out.println("中断");
input.interrupt();//中断线程
}
}
else
{
finish = true;
return;
//System.out.println("Do with parameter:" + parameter);
}
}
public static void main(String[] args) throws Exception
{
Test t = new Test(); }
}
这个正点,我本身也通过多线程实现了这个过程,但就是卡在了这里,用Scanner对象也是一样的
(该用户很聪明,没有设置昵称) ========》》名副其实,牛,夸个 O(∩_∩)O
我怎么就忘了system.exit(0)呢,还有两处调用test()方法很妙,哈哈
你使用了System.exit(0),就是相当于程序处理完了?
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.*;public class Test
{
public String parameter = null;
private boolean finish = false;
public void test()
{
if(finish)
{
return;
}
if(parameter == null)
{
System.out.println("Do without parameter");
}
else
{
System.out.println("Do with parameter:" + parameter);
}
finish = true;
//System.exit(0);
end();
}
public void load() throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
new Thread()
{
public void run()
{
try
{
Thread.sleep(3000);
}
catch(Exception ex)
{
ex.printStackTrace();
}
test();
}
}.start();
parameter = br.readLine();
test();
}
public void end()
{
try
{
Robot t = new Robot();
t.keyPress(KeyEvent.VK_ENTER);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
Test t = new Test();
t.load();
}
}
当没有输入值时,流一直都处于堵塞状态,都不会结束了;
当有输入值时,流的堵塞现象都要等到3秒以后才能结束啊;
不过对于这个问题,你第一次的回答我还是挺满意的,虽然有一点缺陷,呵呵。
谢谢了
1、没有输入时,程序会在3秒后向cmd窗口中输入一个回车,程序运行后就别动了,要不t.keyPress()时cmd窗口没有获得焦点,程序不会结束
2、你感觉有延迟可能是因为创建Robot和执行操作需要时间
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.*;public class Test
{
public String parameter = null;
private boolean finish = false;
Robot t;
public Test()
{
try
{
t = new Robot();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void test()
{
if(finish)
{
return;
}
finish = true;
if(parameter == null)
{
System.out.println("Do without parameter");
}
else
{
System.out.println("Do with parameter:" + parameter);
}
}
public void load() throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
new Thread()
{
public void run()
{
try
{
for(int i = 0; i < 3000; i++)
{
if(finish)
{
return;
}
Thread.sleep(1);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
test();
end();
}
}.start();
parameter = br.readLine();
test();
end();
}
public void end()
{
t.keyPress(KeyEvent.VK_ENTER);
}
public static void main(String[] args) throws Exception
{
Test t = new Test();
t.load();
}
}
private String value; private boolean cancelled; private boolean got; public void run() { try { String line = null; StringBuffer b = new StringBuffer(); while (!cancelled) {
if (System.in.available() > 0) {
char[] cache = new char[System.in.available()]; int c = new InputStreamReader(System.in).read(cache); this.got = true; this.value = new String(cache); break; } else {
Thread.yield(); continue;
}
} } catch (IOException e) {
e.printStackTrace(); this.cancelled = true; } finally {
/* don't close System.in */
}
} public String getValue() {
return this.got ? this.value : null;
} public void cancel() {
this.cancelled = true;
}
} public static void main(String[] args) {
InputThread helper = new InputThread(); System.out.println("Please enter value (time out is 10 seconds):"); helper.start(); try {
helper.join(10 * 1000); helper.cancel(); } catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} String value = helper.getValue(); if (value == null) {
System.out.println("Timed out, use default value instead");
} else {
System.out.println("Entered :" + value);
} }
}
to wd9053: 我测试时出现问题,是由于我失误,呵呵
to humanity:available()方法就解决问题了,看来我对API还是不熟
分不是很多,还请谅解,呵呵