IDEMPIERE-312 Performance: Use JDK ThreadPool API for dynamically created thread

This commit is contained in:
Heng Sin Low 2012-07-02 14:13:58 +08:00
parent c611dd0830
commit cd4d19b183
1 changed files with 23 additions and 17 deletions

View File

@ -32,6 +32,8 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Level; import java.util.logging.Level;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
@ -40,6 +42,8 @@ import javax.swing.event.EventListenerList;
import org.adempiere.base.Core; import org.adempiere.base.Core;
import org.adempiere.base.IColumnCallout; import org.adempiere.base.IColumnCallout;
import org.adempiere.base.Service; import org.adempiere.base.Service;
import org.adempiere.util.ContextRunnable;
import org.compiere.Adempiere;
import org.compiere.util.CLogMgt; import org.compiere.util.CLogMgt;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
@ -192,6 +196,7 @@ public class GridTab implements DataStatusListener, Evaluatee, Serializable
/** Async Loader */ /** Async Loader */
private Loader m_loader = null; private Loader m_loader = null;
private Future<?> m_loaderFuture = null;
/** Async Loading complete */ /** Async Loading complete */
private volatile boolean m_loadComplete = false; private volatile boolean m_loadComplete = false;
/** Is Tab Included in other Tab */ /** Is Tab Included in other Tab */
@ -225,12 +230,12 @@ public class GridTab implements DataStatusListener, Evaluatee, Serializable
/************************************************************************** /**************************************************************************
* Tab loader for Tabs > 0 * Tab loader for Tabs > 0
*/ */
class Loader extends Thread class Loader extends ContextRunnable
{ {
/** /**
* Async Loading of Tab > 0 * Async Loading of Tab > 0
*/ */
public void run() protected void doRun()
{ {
loadTab(); loadTab();
} // run } // run
@ -241,21 +246,18 @@ public class GridTab implements DataStatusListener, Evaluatee, Serializable
*/ */
private void waitLoadCompete() private void waitLoadCompete()
{ {
if (m_loadComplete) if (m_loaderFuture == null || m_loadComplete)
return; return;
// //
m_loader.setPriority(Thread.NORM_PRIORITY);
log.config (""); log.config ("");
while (m_loader.isAlive()) try {
{ m_loaderFuture.get();
try } catch (InterruptedException e) {
{ m_loaderFuture = null;
Thread.sleep(100); // 1/10 sec log.log(Level.SEVERE, e.getLocalizedMessage(), e);
} } catch (ExecutionException e) {
catch (Exception e) m_loaderFuture = null;
{ log.log(Level.SEVERE, e.getLocalizedMessage(), e);
log.log(Level.SEVERE, "", e);
}
} }
log.config ("fini"); log.config ("fini");
} // waitLoadComplete } // waitLoadComplete
@ -275,7 +277,7 @@ public class GridTab implements DataStatusListener, Evaluatee, Serializable
log.fine("#" + m_vo.TabNo + " - Async=" + async + " - Where=" + m_vo.WhereClause); log.fine("#" + m_vo.TabNo + " - Async=" + async + " - Where=" + m_vo.WhereClause);
if (isLoadComplete()) return true; if (isLoadComplete()) return true;
if (m_loader != null && m_loader.isAlive()) if (m_loaderFuture != null && m_loaderFuture.isDone())
{ {
waitLoadCompete(); waitLoadCompete();
if (isLoadComplete()) if (isLoadComplete())
@ -285,11 +287,12 @@ public class GridTab implements DataStatusListener, Evaluatee, Serializable
if (async) if (async)
{ {
m_loader = new Loader(); m_loader = new Loader();
m_loader.start(); m_loaderFuture = Adempiere.getThreadPoolExecutor().submit(m_loader);
return false; return false;
} }
else else
{ {
m_loaderFuture = null;
return loadTab(); return loadTab();
} }
} // initTab } // initTab
@ -341,8 +344,11 @@ public class GridTab implements DataStatusListener, Evaluatee, Serializable
m_vo = null; m_vo = null;
if (m_loader != null) if (m_loader != null)
{ {
if (m_loader.isAlive()) m_loader.interrupt(); if (m_loaderFuture != null && !m_loaderFuture.isDone())
m_loaderFuture.cancel(true);
m_loader = null; m_loader = null;
m_loaderFuture = null;
} }
} // dispose } // dispose