java程序结构
publicabstractclassSwingWorker{privateObjectvalue;privatestaticclassThreadVar{private...
public abstract class SwingWorker {
private Object value;
private static class ThreadVar {
private Thread thread;
ThreadVar(Thread t) {
thread=t;
}
synchronized Thread get() {
return thread;
}
synchronized void clear() {
thread=null;
}
}
private ThreadVar threadVar;
protected synchronized Object getValue() {
return value;
}
private synchronized void setValue(Object x) {
value=x;
}
public abstract Object construct();
public void finished() {
}
public void interrupt() {
Thread t=threadVar.get();
if(t!=null) {
t.interrupt();
}
threadVar.clear();
}
public Object get() {
while(true) {
Thread t=threadVar.get();
if (t==null) {
return getValue();
}
try {
t.join();
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
return null;
}
}
}
public SwingWorker() {
new Runnable() {
public void run() {
finished();
}
};
Runnable doConstruct = new Runnable() {
public void run() {
try {
try {
setValue(construct());
} finally {
threadVar.clear();
}
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
finished();
}
});
} catch (InterruptedException ex) {
Logger.getLogger(SwingWorker.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(SwingWorker.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
Thread t=new Thread(doConstruct);
threadVar=new ThreadVar(t);
}
public void start() {
Thread t=threadVar.get();
if (t!=null) {
t.start();
}
}
}
这种程序结构是关于线程的什么功能,很难看明白;总觉得没什么操作?希望高人解答 谢谢! 展开
private Object value;
private static class ThreadVar {
private Thread thread;
ThreadVar(Thread t) {
thread=t;
}
synchronized Thread get() {
return thread;
}
synchronized void clear() {
thread=null;
}
}
private ThreadVar threadVar;
protected synchronized Object getValue() {
return value;
}
private synchronized void setValue(Object x) {
value=x;
}
public abstract Object construct();
public void finished() {
}
public void interrupt() {
Thread t=threadVar.get();
if(t!=null) {
t.interrupt();
}
threadVar.clear();
}
public Object get() {
while(true) {
Thread t=threadVar.get();
if (t==null) {
return getValue();
}
try {
t.join();
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
return null;
}
}
}
public SwingWorker() {
new Runnable() {
public void run() {
finished();
}
};
Runnable doConstruct = new Runnable() {
public void run() {
try {
try {
setValue(construct());
} finally {
threadVar.clear();
}
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
finished();
}
});
} catch (InterruptedException ex) {
Logger.getLogger(SwingWorker.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(SwingWorker.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
Thread t=new Thread(doConstruct);
threadVar=new ThreadVar(t);
}
public void start() {
Thread t=threadVar.get();
if (t!=null) {
t.start();
}
}
}
这种程序结构是关于线程的什么功能,很难看明白;总觉得没什么操作?希望高人解答 谢谢! 展开
4个回答
展开全部
很长,不看。
看了大半这都是定义,一个线程类里面,有启动线程的方法、获取线程参数地方法……
没有主函数调用,这里当然不能实现具体什么功能,这里是定义
看了大半这都是定义,一个线程类里面,有启动线程的方法、获取线程参数地方法……
没有主函数调用,这里当然不能实现具体什么功能,这里是定义
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
wangyi6912
正解
正解
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有什么问题?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询