package A;
public class GlobalServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//此处不能加同步控制
new BBBB().process();
}
}
package B;
public class BBBBB { public void process() {
//从容器中获取一个文件file
if(!file.exist()) {
this.file2Zip();
}else {
//to do other thing
}
}
//作用:压缩
public void file2Zip() {
//to do something
}
}
如果这样写的话,将可能产生这样的问题,一个线程正在执行压缩,而同时访问另一个线程也将执行压缩操作,而file2Zip() 方法非常耗时,所以我想它变成 生产者与消息者那样,应该如何处理的呢???
public class GlobalServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//此处不能加同步控制
new BBBB().process();
}
}
package B;
public class BBBBB { public void process() {
//从容器中获取一个文件file
if(!file.exist()) {
this.file2Zip();
}else {
//to do other thing
}
}
//作用:压缩
public void file2Zip() {
//to do something
}
}
如果这样写的话,将可能产生这样的问题,一个线程正在执行压缩,而同时访问另一个线程也将执行压缩操作,而file2Zip() 方法非常耗时,所以我想它变成 生产者与消息者那样,应该如何处理的呢???
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货