每次判断服务器线程是否为空,为空就start不就行了,不为空就继续!

解决方案 »

  1.   

    不可能的,死了的线程没法重启;只能
    class Server implements Runnable{
      private Object helperObject;
      Server(Object helper){
         this.helperObject=helper;
      }void run(){
       while(stop==false){
          doSome();
          helperObject.wait();
       }
    }
    当你发现是挂了以后,使用helper.notifyAll()就可以了,想停止时将stop设为true就可以了
    }
      

  2.   

    class ServerTest {    public static void main(String[] args) throws Exception {
            Object helper = new Object();
            Object lock=new Object();
            Server ser = new Server(helper);
            Thread test = new Thread(ser);
            test.start();
            Thread.sleep(2000);
            synchronized(helper) {
                helper.notifyAll();
            }
            Thread.sleep(2000);
            synchronized(helper) {
                helper.notifyAll();
            }
            Thread.sleep(2000);
            ser.setStop(true);
            synchronized(helper) {
                helper.notifyAll();
            }
        }
    }class Server implements Runnable {
        private Object helper;
        private Object lock=new Object();    private boolean stop = false;    /**
         * @return 返回 stop.
         */
        public boolean isStop() {
            return stop;
        }    /**
         * @param stop
         *            The stop to set.
         */
        public void setStop(boolean stop) {
            this.stop = stop;
        }    public Server(Object helper) {
            this.helper = helper;
        }    public void run() {
           while (stop == false) {
                synchronized (helper) {
                    System.out.println(1);
                    try {
                        helper.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
      

  3.   

    /*
     * Created on 2004-8-30
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     *//**
     * @author zhangrzh
     * 
     * TODO To change the template for this generated type comment go to Window -
     * Preferences - Java - Code Style - Code Templates
     */
    class ServerTest {    public static void main(String[] args) throws Exception {
            Object helper = new Object();
            Server ser = new Server(helper);
            Thread test = new Thread(ser);
            test.start();
            Thread.sleep(2000);
            synchronized(helper) {
                helper.notifyAll();
            }
            Thread.sleep(2000);
            synchronized(helper) {
                helper.notifyAll();
            }
            Thread.sleep(2000);
            ser.setStop(true);
            synchronized(helper) {
                helper.notifyAll();
            }
        }
    }class Server implements Runnable {
        private Object helper;    private boolean stop = false;    /**
         * @return 返回 stop.
         */
        public boolean isStop() {
            return stop;
        }    /**
         * @param stop
         *            The stop to set.
         */
        public void setStop(boolean stop) {
            this.stop = stop;
        }    public Server(Object helper) {
            this.helper = helper;
        }    public void run() {
           while (stop == false) {
                synchronized (helper) {
                    System.out.println(1);
                    try {
                        helper.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
      

  4.   

    1、寻找使服务档掉的原因是否是由于程序引起的。
    2、服务档了程序是可以知道的,程序中检测这个状态,如果档了,就再new个线程对象喽。