请问:如何让线程在结束后又重新起动. 每次判断服务器线程是否为空,为空就start不就行了,不为空就继续! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不可能的,死了的线程没法重启;只能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就可以了} 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(); } } } }} /* * 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(); } } } }} 1、寻找使服务档掉的原因是否是由于程序引起的。2、服务档了程序是可以知道的,程序中检测这个状态,如果档了,就再new个线程对象喽。 java多线程读取文件处理 用Java求三角形面积 以下代码错在哪里?怎么改? 关于HelloWorld ******不明白Iterator 的用法 Swing 新手,怎么在JComboBox中显示图标!!! Java能否做到把取得的有格式文字转化成流,再把这个流转化成有格式文字?如果可以,应该怎么做? 哪位大神帮小弟解决下问题啊,谢谢了 三个奇怪的问题需要Java高人帮我解决一下! java文件编译的一个问题? 我要学java,有了JDK,是不是还需要一个集成开发环境,如Jbuilder??? 关于java环境配置的 问题~~~!!谁能帮帮忙?? 学Java已经一个多月了
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就可以了
}
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();
}
}
}
}
}
* 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();
}
}
}
}
}
2、服务档了程序是可以知道的,程序中检测这个状态,如果档了,就再new个线程对象喽。