• Post Reply Bookmark Topic Watch Topic
  • New Topic

optimation of resources plz help

 
vikki
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class AlertThread extends Thread {
public void run() {
while(true) {
Calendar dt = Calendar.getInstance();
for(int i = 0; i < arrLstMtgsLstAlrt.size(); i++) {
String strDate = ((Meeting)arrLstMtgsLstAlrt.get(i)).getDate();
String strName = ((Meeting)arrLstMtgsLstAlrt.get(i)).getName();
int ind = strDate.indexOf("-");
int intMon = Integer.parseInt(strDate.substring(0, ind));
int intDay = Integer.parseInt(strDate.substring(ind + 1, strDate.lastIndexOf("-")));
int intYr = Integer.parseInt(strDate.substring(strDate.lastIndexOf("-") + 1));

if(intDay == dt.get(Calendar.DATE) && intMon == (dt.get(Calendar.MONTH) +1) && intYr == dt.get(Calendar.YEAR)) {
String strAlrt = ((Meeting)arrLstMtgsLstAlrt.get(i)).getAlertTime();
int intHr = Integer.parseInt(strAlrt.substring(0, strAlrt.indexOf(":")));
int intMin = Integer.parseInt(strAlrt.substring(strAlrt.indexOf(":") + 1));
System.err.println("MeetingsListForm alertthread user:" + intHr + ":" + intMin);
if(intHr == dt.get(Calendar.HOUR) && intMin == dt.get(Calendar.MINUTE))
JOptionPane.showMessageDialog(null, "You have a meeting :" + strName);
}
}
}
}
}






/* Please help me to use the limited resources in the thread or without using it*/
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch.

One simple thing you can do to vastly reduce your resource usage would simply be to sleep in the loop. Call Thread.sleep(30000) to sleep for 30 seconds at a time; then you'd only check your list of appointments twice a minute rather than many, many times.

Another: change your Meeting class to store the date as a Date object, or at least as minutes and hours, days and months; in other words, do the parsing of the date string when the Meeting is created, and store it in parsed form. That way it only has to be parsed once, not many times.

Yet another: store your Meetings sorted by date and time, so that you only need to compare the first one to the current time, instead of all of them.

Is the date always guaranteed to have a valid format? If not, you ought to be catching NumberFormatException when you call parseInt, or one bad date will stop this whole Thread.

I wonder if you've given any thought to Thread safety in this program. Can appointments be added or removed from arrLstMtgsLstAlrt while this thread is running? If so, there's a potential for errors (just one example: imagine that the last appointment is removed just before the get() call that retrieves it.
 
vikki
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx man

yes, appointments be added or removed from arrLstMtgsLstAlrt while this thread is running?


then what could be the possible solution??
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread safety is a complex topic; it's impossible to explain it all here. As an introduction, you might read this. But a quick recipe that will work, more or less, would be to use the java.util.Collections.synchronizedList() method to make a synchronized wrapper for the ArrayList, and use that wrapper everywhere instead of the ArrayList itself; and then put loops like your alarm-checking "for" loop inside a "synchronized" block.
 
vikki
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx a lot
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!