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

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Schneier on Security
Schneier on Security
C
Cisco Blogs
Help Net Security
Help Net Security
I
Intezer
Simon Willison's Weblog
Simon Willison's Weblog
Know Your Adversary
Know Your Adversary
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
小众软件
小众软件
Jina AI
Jina AI
量子位
T
Threatpost
Forbes - Security
Forbes - Security
L
LINUX DO - 最新话题
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cyber Attacks, Cyber Crime and Cyber Security
有赞技术团队
有赞技术团队
博客园_首页
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Exploit Database - CXSecurity.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Scott Helme
Scott Helme
博客园 - 【当耐特】
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Last Watchdog
The Last Watchdog
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
SecWiki News
SecWiki News
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
The Cloudflare Blog
S
Schneier on Security
爱范儿
爱范儿
N
News and Events Feed by Topic
NISL@THU
NISL@THU
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 刘刚

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