因为你运行的程序三是另外一个虚拟机实例,两个虚拟机实例中加载的同一个类是不等效的,两个虚拟机之间的交互你可以尝试用RMI或者Socket等

解决方案 »

  1.   

    那有没有除了上面说的RMI或者Socket方式其他的方法来停掉正在运行的线程呢?我的想法是通过另一个程序触发来停止程序二的线程,而不是在程序二中直接调用stopthread()方法(在程序二中直接调用stopthread方法是可以停掉的,已经试过)希望大家指教,这只是我的想法,大家可以帮忙想想有没有其他方式,这个问题困扰好久了。谢谢
      

  2.   

    方案1:
    ThreadTest implements Runnable{
        protected Boolean runflag;
        public synchronized void stopthread()
    {
    runflag =new Boolean(false);
    }
    public synchronized boolean getrunflag()
    {
    return runflag.booleanValue();
    }
             public void run()
    {
                .......
              }
            
    }
    public class ThreadTestStop extends ThreadTest{
          public static void main(String[] args)
    {
    System.out.println("调用停止程序");
    new TreadTest().stopthread();

    }}
      

  3.   

    按错了键竟然提交了。。
    上面的改一下:
    public synchronized boolean getrunflag()
    {
                      if(runflag==null)
                           runflag=new Boolean(true);
    return runflag.booleanValue();
    }
      

  4.   

    怪了,既然是多线程同步,为什么要用synchronized?建议去掉
      

  5.   

    按照上面那位仁兄说的试了一下,还是不行,不知道是不是因为程序一和程序三是两个不同的实例造成的?还是因为其它什么原因?具体程序如下:
    程序一:
    public class ThreadTestMain
    {
    public static void main(String[] args)
    {
    System.out.println("调用启动程序");
    Thread a = new TreadTest();
    a.start();
    }
    }程序二:
    public class TreadTest extends Thread
    {
    protected Boolean runflag; public synchronized void stopthread()
    {
    runflag =new Boolean(false);
    }
    public synchronized boolean getrunflag()
    {
    if(runflag==null)
    runflag=new Boolean(true);
    return runflag.booleanValue(); }
    public void run()
    {
    try
    {
    while (getrunflag())
    {
    System.out.println(Thread.currentThread().getName() + " running");
    try
    {
    System.out.println("..............休息...............");
    Thread.sleep(5 * 1000);
    System.out.println("..............休息结束...............");
    }
    catch (Exception ex)
    {
    System.out.println(ex.getMessage());
    }
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }程序三:
    public class ThreadTestStop 
    { public static void main(String[] args)
    {
    System.out.println("调用停止程序");
    new TreadTest().stopthread();

    }
    }希望大家再帮忙看看,或者还有其它什么方式,多谢了!!!!
      

  6.   

    如果去掉synchronized能获得当前的runflag值吗?但是也是去掉试一下还是不行,是不是因为程序一和程序三启用了不同实例造成的啊?希望各位高手赐教,在线等候!
      

  7.   

    如果你是同一个虚拟机启动的话,建议你更改一下,第一个类
    public class ThreadTestMain
    {
             public static TreadTest a = null ;
    public static void main(String[] args)
    {
    System.out.println("调用启动程序");
    a = new TreadTest();
    a.start();
    }
    };
    在第三个类就可以使用
    程序三:
    public class ThreadTestStop 
    { public static void main(String[] args)
    {
    System.out.println("调用停止程序");
    ThreadTestMain.a.stopthread();

    }
    }
    ==================================
    上面说的是同一虚拟机下启动的情况下;若你不是在同一虚拟机下启动的话,只好使用sokct等通讯方式
      

  8.   

    各位大虾好人做到底,请问用soket方式怎么做,能够给个例子或者讲解一下?不胜感谢!!!
      

  9.   

    谢谢tonyyl的回复与关注,如果照你说的将程序三放到程序一中调用应该怎么做呢?那样的话在程序一中调用程序二的stopthread()将程序停止,总得满足一定条件才能停止吧,但是我的初衷是,当我想停止程序二的时候就触发stopthread()将其停止,这个触发条件不是具体的,所以比较难办,请大家继续帮忙指教啊!!!!谢了~~~
      

  10.   

    public class ThreadTestMain extends Thread
    {public static int flag=0;
    public static void main(String[] args)
    {
    System.out.print("input the 0?:");
    sleep(1000*4);
    System.out.println("调用启动程序");
    if(flag==0){TreadTest a = new TreadTest();a.start();
    flag++;
    }
    if(args[0]=="0"){
          new ThreadStop().stopThread();
    }else{
       main(String args[]);
    }
    }
    public void run(){
    }
    }
      

  11.   

    要是能像C++那样从控制台输入,就简单多了,不知道main()的迭代能不能实现