解决方案 »

  1.   

     main()   你是想调用  public static void main(String[] args) 吧
      

  2.   

    对,代码简写的,编译没问题想的是开两条线程监控输入流,貌似system.in读取后就停了,就这样想的.
      

  3.   

    你启动了两个线程,有可能在你输入了start之后,进入到 while(true)循环体里面的时候另外一个线程抢占了资源。所以就一直处于等待状态。楼主可以把出现的问题描述的更加详细一点。这样别人才能更加清楚的回答,ps:其实我也是一个菜鸟了。
      

  4.   

    Thread start = new Thread(new Test ());if(br.readLine().equals("start")){
      

  5.   

    Thread start = new Thread(new Test());
            Thread stop = new Thread(new  Test());你这个线程是两个空线程啊,你得把自己的线程类弄进去啊。
      

  6.   

    不行啊,完全不理解多线程,完整代码如下,麻烦看下public class Test implements Runnable{
    @Override
    public void run() {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s = null;
    try {
    s = br.readLine();
    if(s.equals("start")){
    while(true){
    System.out.println("Hello");
    Thread.sleep(2000);
    }
    }else if(s.equals("stop")){
    System.exit(0);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static void main(String[] args) {
    Thread thread = new Thread(Test());
    thread.start();
    }
    }
      

  7.   

    谢了,解决了.另外问下,API文档完全看不懂,这正常吗?编程究竟该怎么学,就这点错误弄的我还以为代码逻辑有问题.
      

  8.   

    然后控制台输入 start
     public class T1 implements Runnable {
    public static void main(String[] args) {
    Thread start = new Thread(new T1());
    start.start();
    } @Override
    public void run() {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try {
    if (br.readLine().equals("start")) {
    while (true) {
    System.out.print("Hello");
    Thread.sleep(1000);
    }
    } else {
    System.exit(0);
    }
    } catch (IOException e) {
    e.printStackTrace();
    } catch (InterruptedException e) {
    e.printStackTrace();
    } }
    }
      

  9.   

    public class T1 implements Runnable
    {
    public static void main(String[] args)
    {
    Thread start = new Thread(new T1());
    start.start();
    } @Override
    public void run()
    {
    while (true)
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try
    {
    if (br.readLine().equals("start"))
    { System.out.print("Hello");
    Thread.sleep(1000);
    } else
    {
    System.exit(0);
    }
    } catch (IOException e)
    {
    e.printStackTrace();
    } catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    } }
    }
      

  10.   

    弄了一天还是搞不定,哪位帮我直接写了吧.需求:
    输入start,开始打印,若再次输入start,提示程序已启动
    输入stop,暂停打印
    输入exit,退出程序
    ,确实写晕了
      

  11.   

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;public class Demo implements Runnable {
    private int count = 0;
    private boolean flag = false;
    public void run() {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String msg = null;
    try {
    while ((msg = br.readLine()) != null) {
    if ("start".equals(msg) && count == 0) {
    System.out.println("请继续输入:");
    count++;
    flag = false;
    } else if (count != 0 && "start".equals(msg)) {
    System.out.println("程序已经运行!");
    }
    if ("stop".equals(msg) && flag == false) {
    System.out.println("输入停止!");
    flag = true;
    count = 0;
    } else if ("exit".equals(msg)) {
    System.exit(0);
    }
    if (!"start".equals(msg) && !"stop".equals(msg)
    && !"exit".equals(msg) && flag == false)
    System.out.println(msg);
    else if(flag == true && !"start".equals(msg)){
    System.out.println("程序已经停止,不能打印了,请重新启动程序!");
    }
    }

    } catch (IOException e) {
    } finally {
    if (br != null) {
    try {
    br.close();
    } catch (IOException e) {
    }
    }
    }
    count = 0;
    } public static void main(String[] args) {
    Demo d = new Demo();
    Thread start = new Thread(d);
    // Thread stop = new Thread(d);
    start.start();
    // stop.start();
    }
    }
    写了一个,用单线程的,你这个功能用不着多线程,而且多线程启动了,你不知道究竟输入的是哪个?每次输入的入口不一定相同,
    你的程序最大的问题是将if语句放在了外面,而一旦进入while循环就是死循环,上面否决了多线程的想法,那么在单线程里面你的while循环根本出不去,还怎么输入数据呢?
    不知道是否符合楼主的要求
      

  12.   

    while(true){sysout}里面是一个监听事件,换成直接打印一句sysout没用啊