private volatile int I = 0;
Total private variable length = 0;
Private boolean value completed = false
Public invalid increase () {
Synchronization (this) {
system . out . println(thread . current thread()。 GetName()+ "increasing)); //The auxiliary viewing results can be commented out.
if(++ I & lt; = 1000)
total+= I;
Otherwise {
this.completed = true
this . notifyall(); //Wake up the main thread
}
}
}
public long getTotal(){
Return this.total
}
public boolean isCompleted(){
Returns this.completed
}
}
Public class DigitalIncrease extension thread (
Digital = null
DigitalIncrease {
this.digital = digtal
start();
}
Public invalid operation () {
while(true){
if(digital.isCompleted())
Break; //If the addition operation has been completed, exit the thread.
digital . increase();
}
}
Public static void main(String [] args){
Digital = newdigital ();
digital increase[]diaarray = new digital increase[ 10];
for(int I = 0; I< 10; i++){
DiArray[i]= new number increment (number); //Create 10 threads
}
//If the addition is not completed, let the main thread wait.
Try {
Synchronization (digital) {
And (! digital.isCompleted())
digital.wait()。
}
}catch (InterruptedException e) {
e . printstacktrace();
}
system . out . println(digital . gettotal());
}
}