As an experiment I ran the following code snippet (simplified):
public class SGamePlay extends Activity implements View.OnClickListener
{
Calendar GlobalCal = null;
public void onCreate(Bundle savedInstanceState)
{
GlobalCal = Calendar.getInstance();
}
long time_in_ms()
{
long ans = -1;
long ans2 = -1;
Calendar LocalCal = Calendar.getInstance();
ans = LocalCal.getTimeInMillis();
ans2 = GlobalCal.getTimeInMillis();
Log.e("game","ans="+ans+" ans2="+ans2);
return ans;
}
// much more code here...
}
The time_in_ms() function is called from a sub thread. The problem is that while ans appears to be a correct, constantly updating value, ans2 appears frozen at its initial value. How could this be?
EDIT: I need to resolve this issue because I want to reduce the need for garbage collection in my program.
Calendaris to be thought of as a “mark” in a calendar. It’s more like a point in time, than an interface to the current time. (Why else would it have set-functions?)So, the reason why
ans2is “frozen” is becauseGlobalCal.getTimeInMillis();will always refer to the time when you calledCalendar.getInstance()in the constructor (which you did once).Is the garbage collection the bottle neck of your program? Is the number of
Calendarsthe bottle neck of the GC? Have you profiled your program?Never mind… call
System.currentTimeMillisinstead.