TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
自运行对象是自运行类自动创建的一个在对象内部运行的内部线程。
这个线程一般在构造函数中(或适时)启动,用户不需要关心和启动线
程以及线程工作的细节。下面是创建自运行对象的常用方法:
public class InnerSelfRun extends Object {
private Thread internalThread;
private volatile boolean noStopRequested;
public InnerSelfRun() {
// 初始化工作放在这里
System.out.println("in constructor - initializing...");
// 返回前必须启动线程
noStopRequested = true;
Runnable r = new Runnable() {
public void run() {
try {
runWork();
} catch ( Exception x ) {
x.printStackTrace();
}
}};
internalThread = new Thread(r);
internalThread.start();
}
private void runWork() {
while ( noStopRequested ) {
System.out.println("in runWork() - still going...");
try {
Thread.sleep(700);
} catch ( InterruptedException x ) {
Thread.currentThread().interrupt(); // re-assert interrupt
}
}
}
public void stopRequest() {
noStopRequested = false;
internalThread.interrupt();
}
public boolean isAlive() {
return internalThread.isAlive();
}
}
public class InnerSelfRunMain extends Object {
public static void main(String[] args) {
InnerSelfRun sr = new InnerSelfRun();
try { Thread.sleep(3000); } catch ( InterruptedException x ) { }
sr.stopRequest();
}
}
Squish.java是一个自运行的图像动画组件,请下载分析。
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
源码下载:http://file.javaxxz.com/2014/10/1/004844328.zip |
|