sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi/src/com/nokia/carbide/cpp/internal/pi/manager/BackgroundLoader.java
changeset 2 b9ab3b238396
child 12 ae255c9aa552
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: 
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.carbide.cpp.internal.pi.manager;
       
    19 
       
    20 import java.util.Hashtable;
       
    21 import java.util.Vector;
       
    22 
       
    23 /**
       
    24  * A utility for predictively resolving classes
       
    25  * This can be used to help offset one of the biggest detractors from
       
    26  * the Java language:  Class loading delays.
       
    27  *
       
    28  **/
       
    29 
       
    30 public class BackgroundLoader implements Runnable
       
    31 {
       
    32 	/**
       
    33 	 * Thread used to load classes in the background
       
    34 	 **/
       
    35 
       
    36 	private static Thread controlThread;
       
    37 
       
    38 	/**
       
    39 	 * Storage for the class names still to be resolved
       
    40 	 **/
       
    41 
       
    42 	private static Vector queue;
       
    43 
       
    44 	static
       
    45 	{
       
    46 		try
       
    47 		{
       
    48 			// This is an IBM-specific code that prevents a nasty error in the IBM resource lookup code -JDM
       
    49 			java.text.DateFormat.getDateInstance();
       
    50 		}
       
    51 		catch (Exception e)
       
    52 		{
       
    53 		}
       
    54 
       
    55 		queue = new Vector();
       
    56 	}
       
    57 
       
    58     /**
       
    59      * Private constructor, to enforce singleton status
       
    60      * Creation date: (12/20/99 3:10:32 PM)
       
    61      **/
       
    62 
       
    63     private BackgroundLoader()
       
    64     {
       
    65     }
       
    66 
       
    67     /**
       
    68      * Enqueue a class for background loading
       
    69      * Creation date: (12/20/99 3:23:28 PM)
       
    70      * @param className java.lang.String
       
    71      **/
       
    72 
       
    73     public static void enqueueClass(String className)
       
    74     {
       
    75     	synchronized(queue)
       
    76     	{
       
    77     		queue.addElement(className);
       
    78 
       
    79     		if (controlThread == null)
       
    80     			start();
       
    81     		else
       
    82     			queue.notify();
       
    83     	}
       
    84     }
       
    85 
       
    86     /**
       
    87      * Start loading
       
    88      * Creation date: (12/20/99 3:15:29 PM)
       
    89      **/
       
    90 
       
    91     public void run()
       
    92     {
       
    93     	if (Thread.currentThread() != controlThread)
       
    94     		return;
       
    95 
       
    96     	Hashtable done = new Hashtable();
       
    97 
       
    98     	while (controlThread.isAlive())
       
    99     	{
       
   100     		String className;
       
   101 
       
   102     		synchronized(queue)
       
   103     		{
       
   104     			if (queue.isEmpty())
       
   105     			{
       
   106     				try
       
   107     				{
       
   108     					queue.wait();
       
   109     				}
       
   110     				catch (InterruptedException ie)
       
   111     				{
       
   112     				}
       
   113     			}
       
   114 
       
   115     			if (!queue.isEmpty())
       
   116     			{
       
   117     				className = (String) queue.elementAt(0);
       
   118     				queue.removeElementAt(0);
       
   119     			}
       
   120     			else
       
   121     				continue;
       
   122     		}
       
   123 
       
   124     		if (!done.containsKey(className))
       
   125     		{
       
   126     			try
       
   127     			{
       
   128     				Class resolved = Class.forName(className);
       
   129     				done.put(resolved.getName(), resolved);
       
   130     			}
       
   131     			catch (Exception e)
       
   132     			{
       
   133     				e.printStackTrace();
       
   134     			}
       
   135     		}
       
   136 
       
   137     		className = null;
       
   138     	}
       
   139 
       
   140     	controlThread = null;
       
   141     }
       
   142 
       
   143     /**
       
   144      * Insert the method's description here.
       
   145      * Creation date: (4/5/00 3:24:04 PM)
       
   146      */
       
   147 
       
   148     private static void start()
       
   149     {
       
   150     	controlThread = new Thread(new BackgroundLoader());
       
   151 
       
   152     	try
       
   153     	{
       
   154     		controlThread.setName(Messages.getString("BackgroundLoader.backgroundLoader")); //$NON-NLS-1$
       
   155     		controlThread.setPriority((Thread.MIN_PRIORITY + Thread.NORM_PRIORITY) / 2);
       
   156     	}
       
   157     	catch (Exception e)
       
   158     	{
       
   159     	}
       
   160 
       
   161     	controlThread.start();
       
   162     }
       
   163 }