各位兄弟姐妹们,帮帮忙啊,
我想让Java程序在3秒后有输入值的话就采用输入值运行;
没有输入值的情况下,采用默认值接着运行该怎么做啊??

解决方案 »

  1.   


    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();
    }
    }
      

  2.   

    我在想这句话: parameter = br.readLine();//估计会一直等着输入吧?
      

  3.   

    1#的方法是使用了System.exit(0),造成假象而已,如果还想进行其它的动作,好像还是不行的,比如说还有其它的输入的话......
      

  4.   

    也可以考虑用一个线程读取输入,并放入ArrayList中
      

  5.   


    试了一下,停不了线程~~~~
    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();    }
    }
      

  6.   


    这个正点,我本身也通过多线程实现了这个过程,但就是卡在了这里,用Scanner对象也是一样的
      

  7.   

      wd9053
    (该用户很聪明,没有设置昵称)    ========》》名副其实,牛,夸个 O(∩_∩)O
    我怎么就忘了system.exit(0)呢,还有两处调用test()方法很妙,哈哈
      

  8.   

      同时也很感谢AWUSOFT的帮助
      

  9.   


    你使用了System.exit(0),就是相当于程序处理完了?
      

  10.   

    这个就是唯一的缺陷了,流引起的堵塞会一直存在,直到最后System.exit(0),如果可以,我当然还是希望能在程序接着运行的开始阶段就解决掉这个问题,而不是等到最后在解决掉。这个也就是我还没有结贴的原因,不知道还有没高手解决这个问题,期待中......
      

  11.   

    用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();
        }
    }
      

  12.   

     Robot类方式,我测试了下,好像还是不行啊,
    当没有输入值时,流一直都处于堵塞状态,都不会结束了;
    当有输入值时,流的堵塞现象都要等到3秒以后才能结束啊;
    不过对于这个问题,你第一次的回答我还是挺满意的,虽然有一点缺陷,呵呵。
    谢谢了
      

  13.   

    用BufferedReaderl里ready()的方法判断一下.
      

  14.   

    稍微修改了下,在我的电脑上正常
    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();
        }
    }
      

  15.   

    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);
      }  }
    }
      

  16.   

    非常感谢wd9053、humanity,你们的方法都挺好的,
    to wd9053: 我测试时出现问题,是由于我失误,呵呵
    to humanity:available()方法就解决问题了,看来我对API还是不熟
    分不是很多,还请谅解,呵呵