简体   繁体   English

Java Swing和线程计划

[英]Java Swing and Threads to schedule

I am trying to come up with a concept where in the GUI I can give a query and specify the Pacing and duration.Like I give it 2 mins and 5hrs..which translates like for every 2mins run the given query for the next 5hrs..Though my GUI is ready and able to submit a query...I am unable to bring in the above specified time concept..Though I am sure this can be done through threading...I am finding it difficult to code it...can anyone over here help me with a basic approach to solve this issue ? 我正在尝试提出一个概念,其中在GUI中我可以给出一个查询并指定起搏和持续时间,就像我给它2分钟5小时一样。它的翻译方式像是每2分钟在接下来的5小时内运行给定的查询。尽管我的GUI已经准备就绪并且能够提交查询...我无法引入上述指定的时间概念。尽管我确信这可以通过线程实现...但是我发现很难编写代码。 ..在座的人可以用一种基本的方法来帮助我解决这个问题吗? is there a better and easy approach than threading ? 有没有比穿线更好更好的方法? please help.. 请帮忙..

Taking your example, if you have a query that you want to run every 2 minutes for 5 hours, what you need to do is calculate the number of times you want the query to execute. 以您的示例为例,如果您有一个查询,希望每2分钟运行5小时,那么您需要做的就是计算查询执行的次数。

2 minutes = 120 seconds.
5 hours = 18,000 seconds.

Number of times (iterations) = 18,000 / 120 = 150 iterations.

Therefore, you would need to submit the query 150 times, every 120 seconds (or 120,000 milliseconds). 因此,您将需要每120秒(或120,000毫秒)提交150次查询。

You have to use threads if you want to submit more than one query with your GUI. 如果要通过GUI提交多个查询,则必须使用线程。

Edited to add: Based on assylias's comment, here's some code 编辑添加:基于assylias的注释,下面是一些代码

public void runQuery(int interval, int duration) {
    final Runnable query = new Runnable() {
        @Override
        public void run() {
            // Run query
        }
    };
    ScheduledExecutorService scheduler = Executors
            .newScheduledThreadPool(1);
    final ScheduledFuture<?> queryHandle = scheduler.scheduleAtFixedRate(
            query, 0, interval, TimeUnit.SECONDS);
    scheduler.schedule(new Runnable() {
        @Override
        public void run() {
            queryHandle.cancel(true);
        }
    }, duration, TimeUnit.SECONDS);
}

Just using Thread, here's another version. 仅使用Thread,这是另一个版本。

public void runQuery(int interval, int duration) {
    final Runnable query = new Runnable() {
        @Override
        public void run() {
            // Run query
        }
    };
    int iterations = duration / interval;
    for (int i = 0; i < iterations; i++) {
        new Thread(query).start();
        for (int j = 0; j < interval; j++) {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
            }
        }
    }
}

The Runnable query is just a place holder in these code examples. 在这些代码示例中, Runnable查询只是一个占位符。

You going to have to write a class that implements Runnable and passes the results of your query back to the GUI. 您将必须编写一个实现Runnable的类,并将查询结果传递回GUI。 Since you're not on the Swing Event Dispatch thread, you'll have to use the SwingUtilities.invokeLater() method to actually make GUI changes. 由于您不在Swing Event Dispatch线程上,因此必须使用SwingUtilities.invokeLater()方法实际进行GUI更改。

You would pass an instance of your class to one of these threaded methods to be executed for the interval and duration that you pass. 您可以将类的实例传递给这些线程方法之一,以在传递的间隔和持续时间内执行。

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

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