/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package TestFiveQuestionTwo;import java.util.logging.Level;
import java.util.logging.Logger;/**
*
* @author Administrator
*/
public class Main { /**
* @param args the command line arguments
*/
public static void main(String[] args) {
A target=new A();
Thread thread2=new Thread(target);
thread2.start();
while(target.getStop()==false){}
target.restart();
}}
class A implements Runnable{
boolean stop=false;
A a = null;
boolean getStop(){
return stop;
}
public void run(){
for(int i=0;i<=8;i++){
System.out.println("线程2运行Ing");
if(i==2){
try{System.out.println("线程2wait");
stop=true;
this.hangUP();
}catch(Exception e){}
}
}
}
public synchronized void hangUP()throws InterruptedException{
this.wait(); }
public synchronized void restart(){
try {
Thread.sleep(4000);
System.out.println("开始执行");
} catch (InterruptedException ex) {
Logger.getLogger(A.class.getName()).log(Level.SEVERE, null, ex);
}
this.notifyAll();
}
}
main方法中使用 A.restart()方法时总是没有用
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package TestFiveQuestionTwo;import java.util.logging.Level;
import java.util.logging.Logger;/**
*
* @author Administrator
*/
public class Main { /**
* @param args the command line arguments
*/
public static void main(String[] args) {
A target=new A();
Thread thread2=new Thread(target);
thread2.start();
while(target.getStop()==false){}
target.restart();
}}
class A implements Runnable{
boolean stop=false;
A a = null;
boolean getStop(){
return stop;
}
public void run(){
for(int i=0;i<=8;i++){
System.out.println("线程2运行Ing");
if(i==2){
try{System.out.println("线程2wait");
stop=true;
this.hangUP();
}catch(Exception e){}
}
}
}
public synchronized void hangUP()throws InterruptedException{
this.wait(); }
public synchronized void restart(){
try {
Thread.sleep(4000);
System.out.println("开始执行");
} catch (InterruptedException ex) {
Logger.getLogger(A.class.getName()).log(Level.SEVERE, null, ex);
}
this.notifyAll();
}
}
main方法中使用 A.restart()方法时总是没有用
倒是你的程序已经达到你要的效果了的