1 package org.bsf.remoteIterator.client.tableModel; 2 3 import org.bsf.remoteIterator.client.RemoteIteratorClient; 4 5 import java.rmi.RemoteException ; 6 import java.util.Iterator ; 7 import java.util.Vector ; 8 9 14 public class BackgroundLoadingThread extends Thread { 15 private static Vector _instantiatedThreads = new Vector ( 20, 10 ); 16 17 private RemoteIteratorClient _remoteIteratorClient = null; 18 private DefaultRemoteIteratorTableModel _defaultRemoteIteratorTableModel = null; 19 20 private boolean _shouldStop = false; 21 private boolean _isFirstCall = true; 22 23 27 35 public BackgroundLoadingThread( RemoteIteratorClient p_remoteIteratorClient, 36 DefaultRemoteIteratorTableModel p_tableModel ) { 37 super(); 38 39 _remoteIteratorClient = p_remoteIteratorClient; 40 _defaultRemoteIteratorTableModel = p_tableModel; 41 42 _instantiatedThreads.add( this ); 44 } 45 46 50 public void run() { 51 while ( !_shouldStop ) { 52 int blockSize = _defaultRemoteIteratorTableModel.getDefaultBlockSize(); 55 56 if ( _isFirstCall ) { 57 blockSize = _defaultRemoteIteratorTableModel.getInitialBlockSize(); 58 59 _isFirstCall = false; 61 } 62 63 try { 64 Iterator iterator = _remoteIteratorClient.next( blockSize ); 66 67 if ( _defaultRemoteIteratorTableModel != null ) { 69 _defaultRemoteIteratorTableModel.addRows( iterator ); 70 } 71 72 if ( _remoteIteratorClient.isLast() ) { 74 _defaultRemoteIteratorTableModel.stopLoadingData(); 75 } else { 76 try { 77 int timeToSleepInMs = _defaultRemoteIteratorTableModel.getDelayBetweenRetrieval(); 79 80 if ( timeToSleepInMs > 0 ) { 81 Thread.sleep( timeToSleepInMs ); 82 } 83 } catch( InterruptedException e ) { 84 } 86 } 87 } catch( RemoteException e ) { 88 } 90 } 91 92 removeRemoteIterator(); 94 } 95 96 100 public void stopLoading() { 101 _shouldStop = true; 103 104 _defaultRemoteIteratorTableModel = null; 106 } 107 108 public synchronized void removeRemoteIterator() { 109 stopLoading(); 112 113 try { 114 if ( _remoteIteratorClient != null ) { 115 _remoteIteratorClient.remove(); 117 _remoteIteratorClient = null; 118 119 _instantiatedThreads.remove( this ); 121 } 122 } catch( RemoteException e ) { 123 } 125 } 126 127 131 public static void removeAllRemainingRemoteIterator() { 132 Iterator iterator = _instantiatedThreads.iterator(); 133 134 while ( iterator.hasNext() ) { 135 try { 136 ( (BackgroundLoadingThread) iterator.next() ).removeRemoteIterator(); 137 } catch( Exception e ) { 138 } 140 } 141 } 142 } 143 | Popular Tags |