当前位置:编程学习 > JAVA >>

有关多线程顺序执行的问题

现有如下代码

public ImageUtils(final String url, final int maxWidth, final int maxHeight, final int pageNo,final String userID, final String topic) {

super(new BorderLayout());
System.out.println("url="+url);
JPanel webBrowserPanel = new JPanel(new BorderLayout());
final JWebBrowser webBrowser = new JWebBrowser(null);
webBrowser.setBarsVisible(false);
webBrowser.navigate(url);
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
add(webBrowserPanel, BorderLayout.CENTER);

JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));

webBrowser.addWebBrowserListener(new WebBrowserAdapter() {

// 监听加载进度
public void loadingProgressChanged(WebBrowserEvent e) {
// 当加载完毕时
if (e.getWebBrowser().getLoadingProgress() == 100) {
String result = (String) webBrowser.executeJavascriptWithResult(jsDimension.toString());
int index = result == null ? -1 : result.indexOf(":");
NativeComponent nativeComponent = webBrowser.getNativeComponent();
Dimension originalSize = nativeComponent.getSize();
Dimension imageSize = new Dimension(Integer.parseInt(result.substring(0, index)), Integer.parseInt(result.substring(index + 1)));
imageSize.width = Math.max(originalSize.width,imageSize.width+20);
imageSize.height = Math.max(originalSize.height,imageSize.height);
nativeComponent.setSize(imageSize);
BufferedImage image = new BufferedImage(imageSize.width,imageSize.height, BufferedImage.TYPE_INT_RGB);
nativeComponent.paintComponent(image);
nativeComponent.setSize(originalSize);
// 当网页超出目标大小时
System.out.println("imageSize.width=" + imageSize.width);
System.out.println("maxWidth=" + maxWidth);
System.out.println("imageSize.height=" + imageSize.height);
System.out.println("maxHeight=" + maxHeight);

if (imageSize.width > maxWidth || imageSize.height > maxHeight) {
// 截图部分图形
image = image.getSubimage(0, 0, 780, 460);
}
try {
String fileDir = new PathUtils().getWebInfPath()+"record/"+userID+"/tp"+topic+"/";
File dstFile = new File(fileDir);
if (!dstFile.exists()){
dstFile.mkdirs();
}
System.out.println(fileDir);
// 输出图像
ImageIO.write(image, "jpg", new File(fileDir+pageNo+".jpg"));
System.out.println(fileDir);
System.out.println("图片输出结束!");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

);
add(panel, BorderLayout.SOUTH);

}

for(int i=1; i<33; i++)
{
final String url = new PropertiesUtils().getPropertyValue("DomainName")+"tp"+topic+".html?page="+i;
final int page = i;
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("以DJ组件保存指定网页截图");
frame.getContentPane().add(new ImageUtils(url,780, 460,page,"1","02"), BorderLayout.CENTER);//获取指定url网页快照图片,然后存到相应的目录
frame.setSize(780, 460);
frame.invalidate();
frame.pack();
frame.setVisible(false);
}
});
}
System.out.println("结束");


现在的执行顺序是先输出先循环输出了ImageUtils中的url,接着是“结束”,然后才循环处理ImageUtils中图片保存的业务逻辑

问题:怎样才能做到循环中的业务逻辑(先输出url,然后保存图片)全部顺序执行完成之后再输出最后的那句“结束”

--------------------编程问答-------------------- 给线程加锁。 --------------------编程问答-------------------- 你这个程序是基于事件的。

要么你就把System.out.println("结束");这句话放到事件处理代码里面去。

要么就在事件处理代码中增加回调机制。

要么就得进行阻塞等待(obj.wait()),不过阻塞方案是不被推荐的,万一阻塞的是GUI线程,就全停了。 --------------------编程问答--------------------
引用 1 楼 fangmingshijie 的回复:
给线程加锁。


请问下给线程加锁怎么做?谢谢 --------------------编程问答-------------------- 自己顶下,别沉了 --------------------编程问答-------------------- 要看你是怎么设计的了,比如A执行完返回一个标志,判断A标志是否是true,是就执行B。 --------------------编程问答-------------------- 自己顶下,          芝麻来人 --------------------编程问答-------------------- 顶,5楼正解! --------------------编程问答-------------------- 老实说,完全没看出为非要等待事件保存处理完成后,再输出“结束”并返回主调函数的必要性。

如果你是需要主调程序能判断出是否事件保存图片处理完成的状态,完全可以用一个标志位去记录,没必要阻塞主调线程。 --------------------编程问答-------------------- 楼上大牛啊! --------------------编程问答-------------------- 此画图问题 很耗内存啊 内存没问题吗
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,