Multi-line Cheng is an advantage of Java, java makes programmer OK very convenient undertake multi-line Cheng program is developed. Acquire better property.
About the concept of multi-line Cheng and general multi-line Cheng process designing, if why be reached,realize Runnable interface for instance, why Stop() can be dropped to wait a moment by Deprecated, foundation of this multi-line Cheng process designing before seeing Matrix please or the Java documentation of Sun.
About multi-line Cheng process designing, when to have what here should mention:
1. Since Stop() is not recommended, so how do we stop Cheng of a line? Direct Kill? Here, writer summary compares one kind general stabler also method:
Class Threadtest Extend Thread{
/ / Skip Some Code. .
Boolean Runflag=true;
Public Synchronized Void Stopthread()
{Runflag=false;
}
Public Synchronized Boolean Getrunflag()
{Return Runflag;
}
Public Void Run() {Runflag=true;Try {While (getrunflag()) {Code1; Code2;
/ / Put Your Code Here
}
}
}Catch (IOException E) {E.printStackTrace();
}
System.out.println(this.getClass().getName() " Stopped" );
}
/ / Skip Some Code. .
}
Such, when needing to stop this line Cheng every time, need to call Stopthread() only can.
Have here needed to notice at 2 o'clock:
1) we used Getrunflag() of a synchronous method to obtain current condition, why use this method and be why use this method and to use While(runflag) directly?
Because be in the multi-line Cheng model of Java,this is, have memery block of object of a common, but every object has him back up demesne, changed position when Cheng of a line, the variable that Jvm can not assure to this line Cheng has been changed updates the condition of communal object memery block immediately, likelihood (possibility is not large) create a problem.
So the proposal has good design habit, use synchronous method to obtain current Runflag to be worth.
2) still have a bit, involve the multi-line Cheng of the network especially, if produced network block () happens inside While loop, so, although Runflag condition changes False for instance, as a result of the program by block, line Cheng uses this kind of method is forever won't by inactive.
Cite a case: Than the program of as above face, if Code1 is form of a paragraph of network, if block produced in Code1, the meaning of block cannot get a request namely
Resource, awaiting till the cows come home, this moment, the change of Runflag condition cannot have effect to While circularly, line Cheng won't be stopped.
The author once was participated in many involve the Java form that gets network resource, often encounter the line Cheng problem that poses because of the block of the network.
If your form may involve network block, produce the block that some kind of message accepts likely perhaps. So, do not stop line Cheng please with this kind of method. Specific means watchs the author please an article: Process designing of advanced multi-line Cheng (2) - the monitoring in multi-line Cheng and overtime problem.
Previous12 Next