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

推荐订阅源

博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cyber Attacks, Cyber Crime and Cyber Security
Project Zero
Project Zero
P
Proofpoint News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cisco Blogs
V
Vulnerabilities – Threatpost
G
GRAHAM CLULEY
N
News | PayPal Newsroom
NISL@THU
NISL@THU
雷峰网
雷峰网
J
Java Code Geeks
Latest news
Latest news
aimingoo的专栏
aimingoo的专栏
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cisco Talos Blog
Cisco Talos Blog
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
Spread Privacy
Spread Privacy
V2EX - 技术
V2EX - 技术
S
Schneier on Security
K
Kaspersky official blog
Recent Announcements
Recent Announcements
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
S
SegmentFault 最新的问题
Security Archives - TechRepublic
Security Archives - TechRepublic
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
Scott Helme
Scott Helme
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
量子位
A
Arctic Wolf

博客园 - 刘刚

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