大牛们, 好啊
  问题如下: 线程A有一个任务, 使用的是单例, 我现在用一个main方法启动A线程, 然后想用另外一个main方法
  去终止A线程
   A如下:
public class TestThread extends Thread
{
    public static TestThread testThread;
    
    private static int initData = 1;
    
    private static boolean isDead = true;
    
    public static boolean isDead()
    {
        return isDead;
    }
    
    private TestThread(){}
    
    public static void setDead(boolean isDead)
    {
        TestThread.isDead = isDead;
    }    public static synchronized TestThread getInstance()
    {
        if (null == testThread)
        {
            testThread = new TestThread();
        }
        
        return testThread;
    }    @Override
    public void run()
    {
        while (TestThread.isDead())
        {
            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            System.out.println("The current value is: " + initData ++);
            System.out.println("The thread id is:" +TestThread.getInstance().getId());
        }
        
        System.out.println("The thread is stop");
    }
    
    public void kill()
    {
        this.interrupt();
    }
}我启动A线程的代码如下:
public class StartThread
{
    /**
     * 
     * @param args
     */
    public static void main(String[] args)
    {
        TestThread.getInstance().start();
        //TestThread.getInstance().kill();
    }
}关闭A线程的类如下:
public class StopThreadTest
{
    public static void main(String[] args)
    {
        TestThread.getInstance().kill();
        if (TestThread.getInstance().isInterrupted())
        {
            System.out.println("关了");
        }
        else
        {
            System.out.println("没有关闭");
        }
    }
}但是为什么总是打印没有关闭啊???
 求大牛解决!

解决方案 »

  1.   

    两个JVM上的程序,怎么互相操作 ?一个JVM是一个进程。楼主的这个方法必然不行。可以想想,采用同一个JVM的情况。
      

  2.   

    你这个问题很有趣原因很简单,你启动了两个jvm进程,单例是指一个jvm进程里只有一个实例。
      

  3.   

    你启动了两个JVM,也就是两个进程这两个进程是毫无关联的。
      

  4.   

    呵呵。大家都知道啊,楼主,建议先了解具体的情况,他们是如何进行的。你一个main就已经是处理一件事情了,你的另外一个main是第二个处理事情,就跟两个人去找同一个人解决事情,第一个去借钱,第二个去还钱,你说第二个是还的第一个借的?根本就是不一样的事情。
      

  5.   

    两个JVM不搭边的,怎么控制?
      

  6.   

    可以使用乾坤大挪移大法关闭.
    我觉得你在外部找一个通信标志吧.由线程A去检查这个标志.
    比如数据库中的字段,文件,SOCKET等.