因为你运行的程序三是另外一个虚拟机实例,两个虚拟机实例中加载的同一个类是不等效的,两个虚拟机之间的交互你可以尝试用RMI或者Socket等
解决方案 »
- osgi和maven的问题
- JSP static变量问题 求解
- java中怎样实现闹钟提醒
- WEB系统的Filter
- How to set the allowed mimetypes in the sturts2.1.6
- 请教高手有关Spring+hibernate的问题~!!!!!!!!
- 小弟初来贵地,想向各位大侠问一下关于用java编写oa,都需要哪些技术!
- 请各位给指条路,帮俺走上J2EE!
- 大家对EJB中的“主键”是怎么理解的?谢谢
- JAVAEE 问题:Filter 的用户登录问题,无法在控制台打印相关信息
- Hibernate真是垃圾
- 谁有jasperreport和ireport的下载,网上的链接都不怎么行了啊!
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();
}}
上面的改一下:
public synchronized boolean getrunflag()
{
if(runflag==null)
runflag=new Boolean(true);
return runflag.booleanValue();
}
程序一:
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();
}
}希望大家再帮忙看看,或者还有其它什么方式,多谢了!!!!
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等通讯方式
{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(){
}
}