简体   繁体   English

使用延迟打开浏览器的多个链接

[英]Open multiple link with browser with delay

I did this program that opens multiple link with default browser: 我做了这个用默认浏览器打开多个链接的程序:

ArrayList<String> linkList = new ArrayList<>();
for (int i = 0; i < linkList.size(); i++) {
  ClassRunnable_OpenLink obj = new ClassRunnable_OpenLink ( linkList.get(i) );
  Thread thread = new Thread(obj);
  thread.start();
}
 private class ClassRunnable_OpenLink implements Runnable {
      private URL link;
      private String string;

         public ClassRunnable_OpenLink (String string) {
             this.string = string;
         }

         private void OpenWithBrowser () {
          try {

              link = new URL ( string );
              edu.stanford.ejalbert.BrowserLauncher launcher = null;
          try {
        launcher = new BrowserLauncher(null);
          } catch (    BrowserLaunchingInitializingException | UnsupportedOperatingSystemException ex) { }
          launcher.openURLinBrowser( link );

          } catch (  MalformedURLException ex | IOException | URISyntaxException ex) {  }
        }

         @Override
         public void run() {  
            OpenWithBrowser( );
         }
  }

This works great only if browser (say it's firefox) is already opened, but if it's not, my program only opens the first link and then I have a firefox message that tells me the the browser is still running so I need to close it first. 这只有在浏览器(比如它的firefox)已经打开时才有效,但如果不是,我的程序只打开第一个链接然后我有一条firefox消息告诉我浏览器仍在运行所以我需要先关闭它。 Same thing with chromium. 与铬相同。

So I thought, if I had a way to check when the browser is closed I could use ProcessBuilder to open new firefox process, but I don't know if it's the best way to do this. 所以我想,如果我有办法检查浏览器何时关闭,我可以使用ProcessBuilder打开新的firefox进程,但我不知道这是否是最好的方法。 Besides my java program allows user to select default browser so it could be complicated to use ProcessBuilder in that case. 除了我的java程序允许用户选择默认浏览器,因此在这种情况下使用ProcessBuilder可能会很复杂。

So do you a have any idea to solve my problem? 所以你有任何想法来解决我的问题吗? Maybe I could set a delay between each Thread in this way the system has the time to execute browser process first time, then opening first link and after the browser is running, opening other links, but how about the delay time in seconds? 也许我可以设置每个Thread之间的延迟,这样系统有时间第一次执行浏览器进程,然后打开第一个链接,浏览器运行后,打开其他链接,但延迟时间以秒为单位怎么样? I'm not able to know the time that browser needs to open so it's not a good idea. 我无法知道浏览器需要打开的时间,所以这不是一个好主意。

I hope you can help me. 我希望你能帮助我。 Thanks 谢谢

A workaround to this (without knowing your exact expectations), could be to do the following: 对此的解决方法(不知道您的确切期望)可以是执行以下操作:

  • After first URL open, you could build a delay of for example 10 seconds. 打开第一个URL后,您可以构建一个例如10​​秒的延迟。
  • Then in any consecutive URL openings, you can assume that the browser is now surely open, and start opening the next URL's fast after each other. 然后在任何连续的URL开头,你可以假设浏览器现在肯定是打开的,并开始快速打开下一个URL。

One note though: Always add some delay to opening URL's (not sure if your framework already does this), because else the browser might crash from the number of URL openings. 但需要注意的是: 总是在打开URL时添加一些延迟(不确定你的框架是否已经这样做了),因为浏览器可能会因URL开放次数而崩溃。

UPDATE: You say that Thread.sleep() causes your program to block, this should never happen. 更新:你说Thread.sleep()导致程序阻塞,这应该永远不会发生。

You should always seperate long-during actions from the rest of your program (The (Graphical) User Interface). 在程序的其余部分(图形用户界面)中,您应始终分开长时间操作。

So it would be better to write your 'URL opener facility' in another thread. 因此,最好在另一个线程中编写“URL开启工具”。
You can read more about that here: http://docs.oracle.com/javase/tutorial/essential/concurrency/ 你可以在这里阅读更多相关内容: http//docs.oracle.com/javase/tutorial/essential/concurrency/

Since java 6 you don't have to use 3rd party implementations to open a webpage with the standard browser. 从java 6开始,您不必使用第三方实现来使用标准浏览器打开网页。 Try 尝试

java.awt.Desktop.getDesktop().browse(uri);

Since this maps to the underlying OS functions chances are high multiple calls will work as expected. 由于这映射到底层的OS函数,因此很高的多次调用将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM