Project

General

Profile

« Previous | Next » 

Revision 6613266b

Added by Leszek Koltunski over 8 years ago

Major: the MessageSender thread used to be running all the time in a tight loop!!

View differences:

src/main/java/org/distorted/library/EffectMessageSender.java
50 50
  
51 51
  private static Vector<Message> mList =null;
52 52
  private static EffectMessageSender mThis=null;
53
  private static volatile boolean mPaused;
54
  
53
  private static volatile boolean mNotify = false;
54

  
55 55
///////////////////////////////////////////////////////////////////////////////////////////////////
56 56
   
57 57
  private EffectMessageSender() 
......
62 62

  
63 63
  static void startSending()
64 64
    {
65
    mPaused = false;
66
       
67 65
    if( mThis==null )
68 66
      {
69 67
      mList = new Vector<>();
......
83 81
  
84 82
  static void stopSending()
85 83
    {
86
    mPaused = true;  
84

  
87 85
    }
88 86
  
89 87
///////////////////////////////////////////////////////////////////////////////////////////////////
......
94 92
     
95 93
    while(true)
96 94
      {
97
      if( mList.size()>0 )
95
      //android.util.Log.d("SENDER", "running...");
96

  
97
      while( mList.size()>0 )
98 98
        {
99
        tmp = mList.get(0); 
99
        tmp = mList.remove(0);
100 100
        tmp.mListener.effectMessage(tmp.mMessage, tmp.mEffectID, tmp.mEffectName, tmp.mBitmapID, tmp.mStr);
101
        mList.remove(0);
102 101
        }
103
     
104
      if( mPaused ) 
102

  
103
      synchronized(mThis)
105 104
        {
106
        synchronized(mThis)
105
        if (!mNotify)
107 106
          {
108 107
          try  { mThis.wait(); }
109 108
          catch(InterruptedException ex) { }
110 109
          }
110
        mNotify = false;
111 111
        }
112 112
      }
113 113
    }
......
116 116
        
117 117
  static void newMessage(EffectListener l, EffectMessage m, long id, int name, long bmpID, String str)
118 118
    {
119
    if( mThis!=null )
119
    Message msg = mThis.new Message(l,m,id,name,bmpID,str);
120
    mList.add(msg);
121

  
122
    synchronized(mThis)
120 123
      {
121
      Message msg = mThis.new Message(l,m,id,name,bmpID,str);
122
      mList.add(msg);
124
      mNotify = true;
125
      mThis.notify();
123 126
      }
124 127
    }
125 128
  }
src/main/java/org/distorted/library/message/EffectListener.java
21 21

  
22 22
///////////////////////////////////////////////////////////////////////////////////////////////////
23 23

  
24
import org.distorted.library.DistortedBitmap;
25 24
import org.distorted.library.EffectNames;
26 25

  
27 26
/**
28 27
 * This interface lets users of the Distorted library get notified when something happens to one of the effects.
29
 * To receive the notifications, we first have to register with a call to {@link DistortedBitmap#addEventListener(EffectListener)}.
28
 * To receive the notifications, we first have to register with a call to {@link org.distorted.library.DistortedObject#addEventListener(EffectListener)}.
30 29
 * List of all possible events that can happen is defined in {@link EffectMessage}
31 30
 */
32 31

  
33 32
public interface EffectListener 
34 33
  {
35 34
/**
36
 * Gets called when event of type 'em' happens to effect 'effectID'. 
35
 * Gets called when event of type 'eventType' happens to effect 'effectID'.
37 36
 * 
38 37
 * @param eventType  Type of event that happened.
39 38
 * @param effectID   ID of the effect the event happened to. This ID must have been previously returned by one
40
 *                   of the DistortedBitmap.{deform,distort,move,...} functions.
41
 * @param effectName Name of the effect as defined in EffectNames, e.g. if effectType==EffectNames.MOVE.ordinal(),
42
 *                   then the event happened to a MOVE effect.
43
 * @param bitmapID   the ID of the DistortedBitmap object, as returned by {@link DistortedBitmap#getID()}, this event
44
 *                   happened to. If the object has been created using a copy constructor from another instance of
45
 *                   DistortedBitmap, the ID here will be the one of the original object.
39
 *                   of the DistortedObject.{deform,distort,move,...} functions.
40
 * @param effectName Name of the effect as defined by EffectNames.ordinal()
41
 * @param bitmapID   the ID of the DistortedObject object, as returned by {@link org.distorted.library.DistortedObject#getID()},
42
 *                   this event happened to. If the Object has been created using a copy constructor
43
 *                   from another instance of DistortedObject, the ID here will be the one of the original object.
46 44
 * @param message    Any message string associated with it. 'Failed' event types have one.
47 45
 * @see EffectMessage
48 46
 * @see EffectNames

Also available in: Unified diff