惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
云风的 BLOG
云风的 BLOG
IT之家
IT之家
The Register - Security
The Register - Security
博客园_首页
博客园 - 聂微东
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
The GitHub Blog
The GitHub Blog
博客园 - 【当耐特】
腾讯CDC
F
Full Disclosure
F
Fortinet All Blogs
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
MyScale Blog
MyScale Blog
T
Tailwind CSS Blog
Jina AI
Jina AI
月光博客
月光博客
A
About on SuperTechFans
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Help Net Security
博客园 - Franky
Vercel News
Vercel News
美团技术团队
C
CERT Recently Published Vulnerability Notes
Security Latest
Security Latest
Scott Helme
Scott Helme
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
www.infosecurity-magazine.com
www.infosecurity-magazine.com
SecWiki News
SecWiki News
The Last Watchdog
The Last Watchdog
U
Unit 42
The Cloudflare Blog
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
T
Tenable Blog
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
B
Blog
Webroot Blog
Webroot Blog
A
Arctic Wolf
S
SegmentFault 最新的问题
aimingoo的专栏
aimingoo的专栏
AWS News Blog
AWS News Blog
I
Intezer
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 刘刚

Google Map [导入]Editor中嵌入Editor [导入]GEF中修改默认的FeedBack [导入](接上篇)关于GEF Feedback的补充 [导入]GEF中导视图的使用 [导入]修改GEF中Connection的端点 [导入]Eclipse中Job API使用总结如下三 [导入]Eclipse中Job API使用总结如下一 [导入]Eclipse调试框架的学习与理解二 [导入]Eclipse调试框架的学习与理解一 [导入]使用JET自动生成代码 [导入]定义自己的Common Navigator三 [导入]定义自己的Common Navigator二 [导入]定义自己的Common Navigator一 [导入]Eclipse运行(launcher)框架三 [导入]Eclipse运行(launcher)框架二 [导入]Eclipse运行(launcher)框架一 [导入]Eclipse属性页的支持 [导入]GEF理解系列九
[导入]Eclipse中Job API使用总结如下二
刘刚 · 2008-01-10 · via 博客园 - 刘刚

网站: JavaEye  作者: liugang594  链接:http://liugang594.javaeye.com/blog/154923  发表时间: 2008年01月10日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

1. 资源改变资源改变可以分为两种情况:

            1. UI参与的

            2. 没有UI参与的

   首先介绍没有UI参与的。没有UI参与的时候可以使用以下三种方式: 

     1.老的IWorkspacerun方法      

    使用方式如下:

        final IFile[] files = getSelectedFiles();
        ResourcesPlugin.getWorkspace().run(
             new IWorkspaceRunnable() {
                 public void run(IProgressMonitor monitor)
                throws CoreException {
                    for (int i = 0; i < files.length; i++) {
                          files[i].setContents(
                       modifyContents(files[i].getContents()), 
                           false, true, monitor);
                   }
                }
        }, null);
 
这种方式的问题就在于:
          它将锁定我们的任务直到其他的任务完成了并且将继续锁定后续的修改资源的任务直到它完成了
          如果monitor传入一个null,那么在我们和任务被阴止时,工作台也提供一个对话框,并且不显示进度!
以上造成的问题之一就是程序响应变得很慢了!

    2.新的IWorkspacerun方法      

     使用方式如下:

           final IFile[] files = getSelectedFiles();
            ResourcesPlugin.getWorkspace().run(
               new IWorkspaceRunnable() {
                         public void run(IProgressMonitor monitor)
              throws CoreException {
                     for (int i = 0; i < files.length; i++) {
                   files[i].setContents(
                      modifyContents(files[i].getContents()), 
                                        false, true, monitor);
                  }
                 }
          }, modifyRule(files), IResource.NONE, monitor);
 
这是上面的run方法的改进版。在3.0以后新加的。修改了旧的方式的缺点!应该总是使用新的而不是旧的!

    3.使用IProgressService

     使用第二种方式显然是挺好的,不过我们可以提供给用户更多的交互,例如取消,显示进度等等。这可以通过 

     IProgressService实现,通常的使用如下:

        // Create a runnable that can be passed to the progress service
         final IFile[] files = getSelectedFiles();

         WorkspaceModifyOperation op = new   WorkspaceModifyOperation(

            modifyRule(files)) {
        protected void execute(IProgressMonitor monitor) throws    

            CoreException {
           for (int i = 0; i < files.length; i++) {
              files[i].setContents(

                       modifyContents(files[i].getContents()), 

                                          false, true, monitor);
                  }

          };
      };


     // Use the progess service to execute the runnable
     IProgressService service =   

         PlatformUI.getWorkbench().getProgressService();
     try {
         service.run(true, true, op);
     } catch (InvocationTargetException e) {
        // Operation was canceled
     } catch (InterruptedException e) {
        // Handle the wrapped exception
     }

     其中servicerun方法的前两个参数分别表示:forkcancelable

2. 以上资源改变的第一第二种方式在没有UI参与的情况下可以工作的很好!但是如果有UI的参与,那么可能就会导致问题的发生了!当有UI参与的时候,可以使用第三种方式的另一个方法:runInUI。当然了,我们还可以在一个job中使用syncExec()asyncExec()方法!所以这个问题是可以解决的!还有一个问题:在代码运行期间用户还是什么都不能做,因为progress service会首先显示一个busy的光标,然后如果在一段时间内工作还没有完成,则会显示出一个进度对话框!对于这个问题:这里还有一种选择就是使用:WorkspaceJob。一般使用示例如下:

final IFile[] files = getSelectedFiles();

    WorkspaceJob job = new WorkspaceJob("Modify some files") {

        public IStatus runInWorkspace(IProgressMonitor monitor) 

              throws CoreException {

           for (int i = 0; i < files.length; i++) {
              files[i].setContents(

            modifyContents(files[i].getContents()), 

                                        false, true, monitor);
           }

           return Status.OK_STATUS;

        }

     };

     job.setRule(modifyRule(files));

     job.schedule();

它类似于IWorkspace,不过它的好处就是可以在后台运行!可以允许用户执行其他任务。不好之处就是:与给一个对话框相比,提供回显给用户是更复杂的!
本文的讨论也很精彩,浏览讨论>>

JavaEye推荐

文章来源:http://liugang594.javaeye.com/blog/154923