Это - плохая практика, чтобы отправить агенту сообщение от чего-то, что не является агентом?

Это можно легко найти с помощью поиска Google:

function random_color(format)
{
    var rint = Math.round(0xffffff * Math.random());
    switch(format)
    {
        case 'hex':
            return ('#0' + rint.toString(16)).replace(/^#0([0-9a-f]{6})$/i, '#$1');
            break;

        case 'rgb':
            return 'rgb(' + (rint >> 16) + ',' + (rint >> 8 & 255) + ',' + (rint & 255) + ')';
            break;

        default:
            return rint;
            break;
    }
}

Обновленная версия:

function random_color( format ){
  var rint = Math.floor( 0x100000000 * Math.random());
  switch( format ){
    case 'hex':
      return '#' + ('00000'   + rint.toString(16)).slice(-6).toUpperCase();
    case 'hexa':
      return '#' + ('0000000' + rint.toString(16)).slice(-8).toUpperCase();
    case 'rgb':
      return 'rgb('  + (rint & 255) + ',' + (rint >> 8 & 255) + ',' + (rint >> 16 & 255) + ')';
    case 'rgba':
      return 'rgba(' + (rint & 255) + ',' + (rint >> 8 & 255) + ',' + (rint >> 16 & 255) + ',' + (rint >> 24 & 255)/255 + ')';
    default:
      return rint;
  }
}
10
задан oxbow_lakes 29 June 2009 в 13:45
поделиться

2 ответа

It depends. When you send a message to an actor from non-actor code, an ActorProxy is automatically created and stored in a thread local. This creates a potential memory leak, albeit a very small one, because the ActorProxy won't be GC'd until the thread is GC'd. The ActorProxy essentially enables the non-actor thread to in many ways behave like an Actor, including receiving message.

The bigger problem is if your thread being managed, similar to the way the actor library manages threads, so that what represents a logical context may at one time be on one thread, and at another time be on another thread. A good example of this would be a servlet container. Your logical context may be a servlet or a session, but the ActorProxy is going to be bound to the thread, and thus shared across logical contexts. If your actors aren't replying to the ActorProxy this isn't such a big deal, but if they are it will likely lead to problems because either (a) the replies will potentially be received by the wrong context, or (b) the messages are never received, and thus the previously mentioned small leak becomes a large one as the mailboxes of the ActorProxies fill up.

[Изменить] Хм ... Кажется, у меня проблемы с чтением вопросов! Окружение его блоком актера создает новый объект актера, который будет правильно обработан сборщиком мусора после его завершения. Имейте в виду, что размещение отправленного сообщения в блоке актора означает, что отправка сообщения будет выполнена в новой реакции в другом потоке, а не в потоке, создающем актера.

8
ответ дан 4 December 2019 в 02:26
поделиться

Я не вижу в этом проблем. Если это имеет смысл в вашем коде, то почему бы и нет? Если вы посмотрите на чистую модель акторов, все является актерами, и только актеры общаются друг с другом, если вы можете разработать свой код таким образом ... отлично ... если вы не можете или не хотите, тогда можно отправить сообщения от неактеров актерам.

0
ответ дан 4 December 2019 в 02:26
поделиться
Другие вопросы по тегам:

Похожие вопросы: