小编Aff*_*tus的帖子

Java线程中的计时器

我有一个负责做一些过程的线程.我希望这样做,以便这些处理每3秒完成一次.我已经使用了下面的代码,但是当线程启动时,没有任何反应.我假设当我为我的计时器定义一个任务时,它会ScheduledTask在一个时间间隔内自动执行,但它根本不做任何事情.我错过了什么?

class temperatureUp extends Thread 
{
    @Override
    public void run()
    {
    TimerTask increaseTemperature = new TimerTask(){

        public void run() {
        try {
            //do the processing 
        } catch (InterruptedException ex) {}
        }
    };

    Timer increaserTimer = new Timer("MyTimer");
    increaserTimer.schedule(increaseTemperature, 3000);

    }
};
Run Code Online (Sandbox Code Playgroud)

java multithreading timer

18
推荐指数
3
解决办法
8万
查看次数

Thread.Sleep在Java中的替代品

有人告诉我Thread.Sleep(),有时候使用是一个糟糕的解决方案,人们会希望在同步方法的一个动作循环中产生一些时间间隔.

另一方面,我有两个不同的线程,它们在我的程序的运行时间和一个共享对象中都是活动的,当我在该共享对象中使用Object.wait(long)时,它会导致我的GUI冻结一段时间.

什么是这个问题的更好的解决方案?


更新此部分代码包含一个以GUI开头的线程:

class temperatureUp extends Thread 
    {
        @Override
        public void run()
        {
        while(true)
        {
            try
            {
                GBC.increaseTemp();
                updateSystemStatus();
            }
            catch(Exception ex)
            {
                StringWriter w = new StringWriter();
                ex.printStackTrace(new PrintWriter(w));
                txtLog.setText(w + "\n" + txtLog.getText());
            }
        }
        }
    };
Run Code Online (Sandbox Code Playgroud)

这是共享对象中的同步方法,GBC:

public synchronized void increaseTemp() throws InterruptedException{
    // don't increase the temperature if the boiler 
    // is not turned on...
    while (!isBoilerOn) 
        wait(); 

    // increase the current temperature 
    if ((currentTemp + 1) < MAX_TEMP && currentTemp < desiredTemp) …
Run Code Online (Sandbox Code Playgroud)

java swing multithreading jframe thread-sleep

6
推荐指数
2
解决办法
3268
查看次数

仅替换列表中的元素一次 - Haskell

我想仅在第一次出现时用新值替换列表中的元素.我编写了下面的代码,但使用它,所有匹配的元素都会改变.

replaceX :: [Int] -> Int -> Int -> [Int]
replaceX items old new = map check items where
check item  | item == old = new 
            | otherwise = item
Run Code Online (Sandbox Code Playgroud)

如何修改代码,以便更改只发生在第一个匹配的项目?

谢谢你的帮助!

haskell list

6
推荐指数
1
解决办法
1789
查看次数

更新数组对象值

我想更新我在工厂中保存的全局数组中的一些值.我使用get方法获取数据,但设置函数以某种方式不起作用,并且数组中的值不会更新.我错过了什么?

.factory('messageList', function () {
   var Messages = 
    [
        {   "title":"Cash in", "icon":"ion-social-euro", 
            "dailyValue": "0", "weeklyValue": "0", "monthlyValue": "0", 
            "category": "financial", "active": "true"
        },
        {   "title":"Sales orders", "icon":"ion-social-euro", 
            "dailyValue": "0", "weeklyValue": "0", "monthlyValue": "0", 
            "category": "sales", "active": "true"
        }
    ]

return {
   get: function() {
      return Messages;
   },
   set: function(title, key, newValue) {
       for (var i = 0; i < Messages.length; i++) {
          if(Messages[i].title == title){
            Messages[i].key = newValue;
          }
       }
   }
 }
})
Run Code Online (Sandbox Code Playgroud)

这是我尝试更新控制器中的值的方法:

messageList.set("Sales orders","dailyValue", $Scope.sum);
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angular-promise

4
推荐指数
1
解决办法
3275
查看次数

发布实体状态更改的最佳实践

我有以下型号:

public class Team {
    public Guid Id {get; set;}
    public string Name {get; set;}
    public string League {get; get;}
    public int Rating {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

在系统中创建新团队后,我将事件发布TeamCreated到服务总线:

{
    "MessageId": "33909eaf-56a1-4467-a01a-64b94f10490c"
    "MessageType": "TeamCreated",
    "CreationDate": "20-01-2016",
    "Payload":  {
        "Id": "11111www-56a1-4467-a01a-64b94f000111",
        "Name": "Toronto Maple Leafs",
        "League": "NHL NorthEast",
        "Rating": 100
    }
}
Run Code Online (Sandbox Code Playgroud)

接下来,此条目已修改为以下内容:

{
    "MessageId": "33909eaf-56a1-4467-a01a-64b94f10490c"
    "MessageType": "TeamUpdated",
    "CreationDate": "20-01-2016",
    "Payload":  {
        "Id": "11111www-56a1-4467-a01a-64b94f000111",
        "Name": "Toronto Maple Leafs",
        "League": "NHL NorthEast",
        "Rating": 50
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,他更新的消息仍保留所有属性的值,而不仅仅是已更改的属性,即团队的等级.

我的模型,在真实系统中有超过50个属性,我不希望在每个属性更新时为它们创建单独的事件.特别是因为可能是在一次更新中更改了多个属性的情况.

在事件采购架构中是否存在针对此场景的已定义模式?

domain-driven-design cqrs event-sourcing

2
推荐指数
1
解决办法
153
查看次数