English 中文(简体)
附表3 B. 实际时间变化的任务
原标题:ScheduledThreadPoolExecutor changing tasks in real time
  • 时间:2012-01-14 14:27:53
  •  标签:
  • java
  • rmi

采用<代码>的Im 下表所示为每30或60秒执行任务的班次。 我希望能够根据<条码>isRmi的价值,改变将在“实时”内执行的任务。 变化不定,但似乎无法工作。 在我申请开始时,变量是根据用户投入确定的,但即便在方案执行期间对变量进行了修改,它仍然执行同样的任务。 你们能否帮助我?

   public void execute() {

        ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(3);

        scheduler.scheduleAtFixedRate(new ServPresTimer(player), 0, 30, TimeUnit.SECONDS);

        if (!isRMI) {
            scheduler.scheduleAtFixedRate(new P2PTimer(player), 1, 60, TimeUnit.SECONDS);
        } else {
            scheduler.scheduleAtFixedRate(new RMITimer(player), 1, 60, TimeUnit.SECONDS);
        }
    }
最佳回答

不是安排两个不同的任务(P2P和RMI),而是安排一个单一任务(P2POrRMI),根据<代码>isRM变量的价值,执行P2P的任务,或海事委员会执行的任务。 你们应该能够完成这项P2POrRMI的任务,仅仅把任务交给海事或P2P:

public class P2POrRMITimer implements Runnable {
    private Runnable p2p;
    private Runnable rmi;
    private ObjectWhichContainsTheFlag flagContainer;

    public P2POrRMITimer(Runnable p2p, 
                         Runnable rmi, 
                         ObjectWhichContainsTheFlag flagContainer) {
        this.p2p = p2p;
        this.rmi = rmi;
        this.flagContainer = flagContainer;
    }

    @Override
    public void run() {
        if (flagContainer.isRmi()) {
            rmi.run();
        }
        else {
            p2p.run();
        } 
    }
}

...

scheduler.scheduleAtFixedRate(new P2POrRMITimer(new P2PTimer(player),
                                                new RMITimer(player),
                                                this),
                              1, 
                              60, 
                              TimeUnit.SECONDS);

• 确保适当同步使用ERM系统,或使其变幻莫测,因为该系统将由一个透镜确定,由另一个读(路透镜)

问题回答

我怀疑你有一些记忆问题,试图:

private volatile boolean isRMI;

<代码>挥发性在此至关重要。 问题在于,在多面环境下,对一层的变数的改变可能不会立即(或永远不会!)在其他深层可见。 <代码>volatus使所有变化立即可见于每一个线上。

您也可使用





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...