private Set<Thread> collectCurrentThreads() { Thread[] activeThreadsT = new Thread[Thread.activeCount()]; Thread.enumerate(activeThreadsT); Set<Thread> res = new HashSet<Thread>(); for (Thread t : activeThreadsT) res.add(t); return res; }
private boolean hasRetrievers() { Thread[] threads = new Thread[Thread.activeCount()]; int numThreads = Thread.enumerate(threads); for (int i = 0; i < numThreads; i++) { if (threads[i].getName().startsWith(RUNNING_THREAD_NAME_PREFIX)) return true; } return false; }
private static void listThreads() { int ac = Thread.activeCount(); if (ac > 0) { Thread[] tlist = new Thread[ac]; Thread.enumerate(tlist); for (Thread t : tlist) { System.out.println("Thread [" + t + "] => {" + t.getClass().getName() + "}"); } } }
@Override public String format(LogRecord record) { int count = Thread.activeCount(); Thread th[] = new Thread[count]; // returns the number of threads put into the array Thread.enumerate(th); String thrName = null; // prints active threads for (int i = 0; i < count; i++) { if (th[i] != null && th[i].getId() == record.getThreadID()) { thrName = th[i].getName(); break; } } if (thrName == null) { thrName = String.format("id:%d", record.getThreadID()); } StringBuilder sb = new StringBuilder(); sb.append(new Date(record.getMillis())) .append(" ") .append(record.getLevel().getLocalizedName()) .append(" ") .append("[") .append(record.getSourceClassName()) .append("]") .append("{") .append(thrName) .append("}: ") .append(formatMessage(record)) .append(LINE_SEPARATOR); if (record.getThrown() != null) { try { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); record.getThrown().printStackTrace(pw); pw.close(); sb.append(sw.toString()); } catch (Exception ex) { // ignore } } return "RT: " + sb.toString(); }
protected void endIdegaDatabasePool() { PoolManager.getInstance().release(); int count = Thread.activeCount(); Thread[] threads = new Thread[count]; Thread.enumerate(threads); for (int i = 0; i < threads.length; i++) { Thread thread = threads[i]; String name = (thread == null) ? null : thread.getName(); // System.out.println(name); if (name != null && name.startsWith("HSQLDB")) { thread.interrupt(); } } }
@Override public void run() { boolean loop = true; try { while (loop) { final Thread[] all = new Thread[Thread.activeCount() + 100]; final int count = Thread.enumerate(all); loop = false; for (int i = 0; i < count; i++) { final Thread t = all[i]; // daemon: skip it. if (t.isDaemon()) continue; // RMI Reaper: skip it. if (t.getName().startsWith("RMI Reaper")) continue; if (t.getName().startsWith("DestroyJavaVM")) continue; // Non daemon, non RMI Reaper: join it, break the for // loop, continue in the while loop (loop=true) loop = true; try { // Found a non-daemon thread. Wait for it. t.join(); } catch (Exception ex) { ex.printStackTrace(); } break; } } // We went through a whole for-loop without finding any thread // to join. We can close cs. } catch (Exception ex) { ex.printStackTrace(); } finally { try { // if we reach here it means the only non-daemon threads // that remain are reaper threads - or that we got an // unexpected exception/error. // if (cs != null && cs.isActive()) { cs.stop(); } } catch (Exception ex) { ex.printStackTrace(); } } }
// Enumerate all active threads private Thread[] enumerateThreads() { boolean fits = false; int inc = 50; Thread[] threads = null; int nrThreads = 0; while (!fits) { try { threads = new Thread[Thread.activeCount() + inc]; nrThreads = Thread.enumerate(threads); fits = true; } catch (ArrayIndexOutOfBoundsException exp) { inc += 50; } } // Trim array Thread ret[] = new Thread[nrThreads]; System.arraycopy(threads, 0, ret, 0, nrThreads); return ret; }
// @Test public void rawEpochStealing() throws Exception { RealEventHubUtilities utils = new RealEventHubUtilities(); utils.setup(-1); int clientSerialNumber = 0; while (true) { Thread[] blah = new Thread[Thread.activeCount() + 10]; int actual = Thread.enumerate(blah); if (actual >= blah.length) { System.out.println("Lost some threads"); } int parkedCount = 0; String selectingList = ""; boolean display = true; for (int i = 0; i < actual; i++) { display = true; StackTraceElement[] bloo = blah[i].getStackTrace(); String show = "nostack"; if (bloo.length > 0) { show = bloo[0].getClassName() + "." + bloo[0].getMethodName(); if (show.compareTo("sun.misc.Unsafe.park") == 0) { parkedCount++; display = false; } else if (show.compareTo("sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0") == 0) { selectingList += (" " + blah[i].getId()); display = false; } } if (display) { System.out.print(" " + blah[i].getId() + ":" + show); } } System.out.println("\nParked: " + parkedCount + " SELECTING: " + selectingList); System.out.println("Client " + clientSerialNumber + " starting"); EventHubClient client = EventHubClient.createFromConnectionStringSync(utils.getConnectionString().toString()); PartitionReceiver receiver = client .createEpochReceiver( utils.getConsumerGroup(), "0", PartitionReceiver.START_OF_STREAM, 1) .get(); boolean useReceiveHandler = false; if (useReceiveHandler) { Blah b = new Blah(clientSerialNumber++, receiver, client); receiver.setReceiveHandler(b).get(); // wait for messages to start flowing b.waitForReceivedMessages().get(); } else { receiver.receiveSync(1); System.out.println("Received a message"); } // Enable these lines to avoid overlap /* */ try { System.out.println("Non-overlap close of PartitionReceiver"); if (useReceiveHandler) { receiver.setReceiveHandler(null).get(); } receiver.close().get(); } catch (InterruptedException | ExecutionException e) { System.out.println( "Client " + clientSerialNumber + " failed while closing PartitionReceiver: " + e.toString()); } try { System.out.println("Non-overlap close of EventHubClient"); client.close().get(); } catch (InterruptedException | ExecutionException e) { System.out.println( "Client " + clientSerialNumber + " failed while closing EventHubClient: " + e.toString()); } System.out.println("Client " + clientSerialNumber + " closed"); /* */ System.out.println("Threads: " + Thread.activeCount()); } }
@Override protected void onPreExecute() { Thread.currentThread().setPriority(Thread.MAX_PRIORITY); Log.d(TAG, "threads: " + Thread.activeCount()); super.onPreExecute(); Log.d(TAG, "onPreExecute"); }
@Before public void setup() throws Exception { int ret; // // adding a delete before creating a keystore // try { if (ksFile != null) { if (ksFile.exists()) { System.out.println( "Keystore File [" + ksFile.getAbsolutePath() + "] is available - and deleting"); ksFile.delete(); System.out.println("Keystore File [" + ksFile.getAbsolutePath() + "] is deleted."); } else { System.out.println("Keystore File [" + ksFile.getAbsolutePath() + "] is not available"); } } else { System.out.println("Keystore File is NULL"); } } catch (Throwable t) { t.printStackTrace(); } Configuration conf = new Configuration(); CredentialShell cs = new CredentialShell(); cs.setConf(conf); try { ret = cs.run(argsCreate); } catch (Exception e) { throw e; } assertEquals(0, ret); System.out.println("(1) Number of active Threads : " + Thread.activeCount()); listThreads(); }
public static void main(String[] args) throws IOException { final WebCrawler wc = new WebCrawler(); Word.readWordList(); for (String word : Word.wordList) { wc.addUrl(Word.wordUrl + word, 1); } long start = System.currentTimeMillis(); System.out.println("开始爬虫……"); wc.begin(); while (true) { StringBuffer response = null; synchronized (wc.responseList) { if (!wc.responseList.isEmpty()) { response = wc.responseList.get(0); wc.responseList.remove(0); } } if (response != null) { // got a response new Word(response); } // 所有爬虫线程都处于等待状态,而且url池已经空了 if (wc.uncrawledUrlList.isEmpty() && Thread.activeCount() == 1 || wc.waitingThreadCount == wc.threadNumber) { long end = System.currentTimeMillis(); System.out.println("总共抓取了" + wc.allUrlList.size() + "个网页。"); System.out.println("总共耗时" + (end - start) / 1000 + "秒。"); System.exit(1); } } }
public static void main(String[] args) { ExecutorTest executorTest = new ExecutorTest(); executorTest.test(); System.out.println("Number of active threads is : " + Thread.activeCount()); System.out.println("Main thread will exit..."); }
public void execute(Scanner scanner) { boolean isReloading = CronFireSettings.getSettingBoolean("_internal_is_reloading", false); // Don't reload if we're already reloading. if (isReloading) return; // Pause (save original state) boolean isPaused = queue.isPaused(); queue.pause(true); CronFireSettings.setSetting("_internal_is_reloading", "true"); // Wait for all jobs to finish int numThreads = Thread.activeCount() - 3; // Compensate for built-in int numWaits = 0; if (numThreads > 0) { System.out.println("Waiting for running jobs to finish..."); while (numThreads > 0) { numThreads = Thread.activeCount() - 3; try { Thread.sleep(250); } catch (Exception e) { } if (numWaits > 1200) { System.out.println("Waiting too long for jobs to finish... aborting RELOAD"); CronFireSettings.setSetting("_internal_is_reloading", "false"); return; } } System.out.println("Reloading..."); } // Reload config and URL files (merge changes) queue.empty(); CronFireSettings.loadConfigFile(CronFireSettings.getSetting("config_file")); CronFireSettings.loadUrls(CronFireSettings.getSetting("urls_file")); // Unpause (or resume original state) queue.pause(isPaused); System.out.println("Reloaded..."); CronFireSettings.setSetting("_internal_is_reloading", "false"); }
private void acceptConnections() { for (; ; ) { try { String handlerName = fullName + "-Handler-" + Thread.activeCount(); new Thread(new ClientHandler(socket.accept()), handlerName).start(); } catch (Exception ex) { Log.warn(fullName + ": exception accepting connection.", ex); } } }
public void printStats(String status) { // String str = Thread.currentThread().getName().split("@@")[0]; // Thread.currentThread().setName(String.valueOf(Thread.currentThread().getId())); LOG.info( "{} {} {} [Active Threads: {}] ", Thread.currentThread().getId(), Thread.currentThread().getName(), status, Thread.activeCount()); }
@Test public void testCredentialProvider() { // test credential provider is registered and return credential providers. cp = new RangerCredentialProvider(); providers = cp.getCredentialProviders(url); if (providers != null) { assertTrue(url.equals(providers.get(0).toString())); } System.out.println("(2) Number of active Threads : " + Thread.activeCount()); listThreads(); }
/** * Runs the same model many times and ensures it is not creating many threads which would finish * blocking the JVM */ @Test public void testModelUsageOfThreads() { // first runs after which the reference will be taken final int HEATING_COUNT = 2; final int TEST_COUNT = 10; // heading period for (int i = 0; i < HEATING_COUNT; i++) runModel(); // take reference final int activeThreads = Thread.activeCount(); final Set<Thread> activeThreadsSet = collectCurrentThreads(); // run many for (int i = 0; i < TEST_COUNT; i++) { System.err.println("exec " + i + " threads " + Thread.activeCount()); runModel(); } if (Thread.activeCount() > activeThreads) { double nbPerIteration = Thread.activeCount() / TEST_COUNT; Set<Thread> threadsNow = collectCurrentThreads(); threadsNow.removeAll(activeThreadsSet); for (Thread t : threadsNow) { System.err.println("extra thread: " + t); t.stop(); } Thread.yield(); System.out.println("did cleaning help ? " + Thread.activeCount() + " / " + activeThreads); threadsNow = collectCurrentThreads(); threadsNow.removeAll(activeThreadsSet); for (Thread t : threadsNow) { System.out.println("extra thread: " + t); } fail( "about " + nbPerIteration + " novel threads are created at each run; this cannot be used in production"); } }
@Test public void testCredentialString() { // test credential provider created is returning the correct credential string. cp = new RangerCredentialProvider(); providers = cp.getCredentialProviders(url); if (providers != null) { assertTrue( "PassworD123".equals(new String(cp.getCredentialString(url, "TestCredential001")))); } System.out.println("(3) Number of active Threads : " + Thread.activeCount()); listThreads(); }
private String readFileHeader(String filename) { String value; try { value = WaarpStringUtils.readFileException(filename); } catch (InvalidArgumentException e) { logger.error("Error while trying to open: " + filename, e); return ""; } catch (FileTransferException e) { logger.error("Error while trying to read: " + filename, e); return ""; } StringBuilder builder = new StringBuilder(value); WaarpStringUtils.replace( builder, REPLACEMENT.XXXLOCALXXX.toString(), Integer.toString(Configuration.configuration.getLocalTransaction().getNumberLocalChannel()) + " " + Thread.activeCount()); WaarpStringUtils.replace( builder, REPLACEMENT.XXXNETWORKXXX.toString(), Integer.toString( Configuration.configuration.getLocalTransaction().getNumberLocalChannel())); WaarpStringUtils.replace( builder, REPLACEMENT.XXXHOSTIDXXX.toString(), Configuration.configuration.getHOST_ID()); if (authentHttp.isAuthenticated()) { WaarpStringUtils.replace( builder, REPLACEMENT.XXXADMINXXX.toString(), Messages.getString("HttpSslHandler.1")); // $NON-NLS-1$ } else { WaarpStringUtils.replace( builder, REPLACEMENT.XXXADMINXXX.toString(), Messages.getString("HttpSslHandler.0")); // $NON-NLS-1$ } TrafficCounter trafficCounter = Configuration.configuration.getGlobalTrafficShapingHandler().trafficCounter(); WaarpStringUtils.replace( builder, REPLACEMENT.XXXBANDWIDTHXXX.toString(), Messages.getString("HttpSslHandler.IN") + (trafficCounter.lastReadThroughput() >> 20) + //$NON-NLS-1$ Messages.getString("HttpSslHandler.MOPS") + //$NON-NLS-1$ Messages.getString("HttpSslHandler.OUT") + //$NON-NLS-1$ (trafficCounter.lastWriteThroughput() >> 20) + Messages.getString("HttpSslHandler.MOPS")); // $NON-NLS-1$ WaarpStringUtils.replace(builder, REPLACEMENT.XXXLANGXXX.toString(), lang); return builder.toString(); }
@Override // Perform actual service task - runs in new thread protected Void doInBackground(Void... params) { Log.i(this.getClass().getName(), "Current Threadname " + Thread.currentThread().getName()); Log.i(this.getClass().getName(), "Current Thread count" + Thread.activeCount()); for (int i = 1; i <= FIBONACCI_LIMIT; i++) { Log.i(this.getClass().getName(), "Fibonacci number " + i + ": " + fib(i)); // Update progress publishProgress((int) ((i / (float) FIBONACCI_LIMIT) * 100)); } publishProgress(100); return null; }
@After public void teardown() throws Exception { System.out.println("In teardown : Number of active Threads : " + Thread.activeCount()); int ret; Configuration conf = new Configuration(); CredentialShell cs = new CredentialShell(); cs.setConf(conf); try { ret = cs.run(argsDelete); } catch (Exception e) { throw e; } assertEquals(0, ret); listThreads(); }
public void run() { try { HostNodeDao nodeDao = new HostNodeDao(); // 得到被监视的防火墙设备 List nodeList = nodeDao.loadNetwork(8); for (int i = 0; i < nodeList.size(); i++) { HostNode node = (HostNode) nodeList.get(i); } Vector vector = null; int numTasks = nodeList.size(); int numThreads = 200; try { List numList = new ArrayList(); TaskXml taskxml = new TaskXml(); numList = taskxml.ListXml(); for (int i = 0; i < numList.size(); i++) { Task task = new Task(); BeanUtils.copyProperties(task, numList.get(i)); if (task.getTaskname().equals("netthreadnum")) { numThreads = task.getPolltime().intValue(); } } } catch (Exception e) { e.printStackTrace(); } // 生成线程池 ThreadPool threadPool = null; if (nodeList != null && nodeList.size() > 0) { threadPool = new ThreadPool(nodeList.size()); // 运行任务 for (int i = 0; i < nodeList.size(); i++) { threadPool.runTask(createTask((HostNode) nodeList.get(i))); } // 关闭线程池并等待所有任务完成 threadPool.join(); threadPool.close(); } threadPool = null; } catch (Exception e) { e.printStackTrace(); } finally { SysLogger.info("********Firewall Thread Count : " + Thread.activeCount()); } }
/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Thread[] threads = new Thread[THREADS_COUNT]; final Object o = new Object(); for (int i = 0; i < THREADS_COUNT; i++) { threads[i] = new Thread( new Runnable() { @Override public void run() { // TODO Auto-generated method stub synchronized (o) { for (int i = 0; i < 1000; i++) { increase(); try { // Thread.sleep(2); o.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }); threads[i].start(); } while (Thread.activeCount() > 1) { synchronized (o) { o.notify(); } Thread.yield(); } System.out.println(race); }
private void createScene() { // defining the axes NumberAxis xAxis = new NumberAxis(0, 105, 10); NumberAxis yAxis = new NumberAxis(); xAxis.setLabel("Quality %"); yAxis.setLabel("User Interactions"); yAxis.forceZeroInRangeProperty(); // xAxis.setMinorTickCount(0); // yAxis.setMinorTickCount(0); // creating the chart if (chartType == null) { chartType = Constant.AREA_CHART; // overlapping area colors this.chart = new AreaChart<Number, Number>(xAxis, yAxis); // this.chart = new StackedAreaChart<Number, Number>(xAxis, yAxis); } else if (chartType.equals(Constant.LINE_CHART)) { this.chart = new LineChart<Number, Number>(xAxis, yAxis); } else { // overlapping area colors this.chart = new AreaChart<Number, Number>(xAxis, yAxis); // this.chart = new StackedAreaChart<Number, Number>(xAxis, yAxis); } if (logger.isDebugEnabled()) logger.debug("Chart created: " + this.chart); chart.setAnimated(true); scene = new Scene(chart, 600, 400); chart .getStylesheets() .add(this.getClass().getResource(Constant.FOLDER_CONF + "styleChart.css").toExternalForm()); createSeries(chart); // seriesOs = FXCollections.observableArrayList(); // createObservableSeries(); // chart.setData(seriesOs); chart.setLegendSide(Side.TOP); logger.debug("Thread chart active: " + Thread.activeCount()); }
public void run() { ByteBuffer buf = ByteBuffer.allocate(UDPMessageSender.MAX_MSG_SIZE); InetSocketAddress srcAddr = null; while (true) { // receive buf.clear(); try { srcAddr = (InetSocketAddress) this.sock.receive(buf); } catch (IOException e) { logger.log( Level.WARNING, "DatagramSocket#receive() threw an Exception and the receiver will die."); return; } buf.rewind(); logger.log(Level.INFO, "Source address: " + srcAddr); // construct a message Message msg; try { msg = Message.decode(buf); } catch (IOException e) { logger.log(Level.WARNING, "Could not decode the received message (corrupted ?).", e); continue; } // check signature byte[] acceptableSig = this.provider.getMessageSignature(); byte[] sig = msg.getSignature(); /* System.out.println("signature:"); System.out.print(" acceptable:"); for (int i = 0; i < acceptableSig.length; i++) System.out.print(" " + Integer.toHexString(acceptableSig[i] & 0xff)); System.out.println(); System.out.print(" message: "); for (int i = 0; i < acceptableSig.length; i++) System.out.print(" " + Integer.toHexString(sig[i] & 0xff)); System.out.println(); */ if (!Signature.match(sig, acceptableSig)) continue; // invoke a Thread handling the incoming Message Runnable r = new UDPMessageHandler(srcAddr, msg); try { if (this.config.getUseThreadPool()) { // note: does not register to handlerThreads this.provider.getThreadPool().submit(r); } else { Thread thr = new Thread(r); thr.setDaemon(false); handlerThreads.add(thr); thr.start(); } } catch (OutOfMemoryError e) { logger.log(Level.SEVERE, "# of threads: " + Thread.activeCount(), e); // synchronized (UDPMessageReceiver.class) { // if (!UDPMessageReceiver.oomPrinted) { // UDPMessageReceiver.oomPrinted = true; // // Thread[] tarray = new Thread[Thread.activeCount()]; // Thread.enumerate(tarray); // for (Thread t: tarray) if (t != null) System.out.println("Th: " + t.getName()); // System.out.flush(); // } // } throw e; } } // while (true) }
/** * Runs the same model many times in parallel and ensures it is still giving relevant results * would finish blocking the JVM */ @Test public void testModelParallel() { ListsOfMessages.getGenlabMessages() .setFilterIgnoreBelow(MessageLevel.TRACE, MessageLevel.TRACE); int countThreadBeginning = Thread.activeCount(); final int PARALLELL_THREADS = 10; class ThreadRunModel extends Thread { private final Set<ThreadRunModel> threadsToWait; public ThreadRunModel(Set<ThreadRunModel> threadsToWait) { setDaemon(false); this.threadsToWait = threadsToWait; } @Override public void run() { for (int i = 0; i < 30; i++) { System.err.println("wait"); // wait a bit randomly try { Thread.sleep((long) (2000 * Math.random())); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // run the model System.err.println("run !"); Map<String, Object> result = runModel(); checkResult(result); } // return synchronized (threadsToWait) { this.threadsToWait.remove(this); } } } final Set<ThreadRunModel> threadsToWait = new HashSet<ThreadRunModel>(); // create threads for (int i = 0; i < PARALLELL_THREADS; i++) { threadsToWait.add(new ThreadRunModel(threadsToWait)); } // start threads for (ThreadRunModel t : threadsToWait) { t.start(); } // wait for all of them to finish while (true) { synchronized (threadsToWait) { if (threadsToWait.isEmpty()) break; System.out.println("waiting for " + threadsToWait.size() + " model threads"); } try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println( "thread count: before " + countThreadBeginning + ", after " + Thread.activeCount()); }
public String getServerStatus() { int playerCount = 0, objectCount = 0; int max = LoginServerThread.getInstance().getMaxPlayer(); playerCount = L2World.getInstance().getAllPlayersCount(); objectCount = L2World.getInstance().getVisibleObjectsCount(); int itemCount = 0; int itemVoidCount = 0; int monsterCount = 0; int minionCount = 0; int minionsGroupCount = 0; int npcCount = 0; int charCount = 0; int pcCount = 0; int detachedCount = 0; int doorCount = 0; int summonCount = 0; int AICount = 0; for (L2Object obj : L2World.getInstance().getVisibleObjects()) { if (obj == null) { continue; } if (obj instanceof L2Character) { if (((L2Character) obj).hasAI()) { AICount++; } } if (obj instanceof L2ItemInstance) { if (((L2ItemInstance) obj).getItemLocation() == ItemLocation.VOID) { itemVoidCount++; } else { itemCount++; } } else if (obj instanceof L2MonsterInstance) { monsterCount++; if (((L2MonsterInstance) obj).hasMinions()) { minionCount += ((L2MonsterInstance) obj).getMinionList().countSpawnedMinions(); minionsGroupCount += ((L2MonsterInstance) obj).getMinionList().lazyCountSpawnedMinionsGroups(); } } else if (obj instanceof L2Npc) { npcCount++; } else if (obj instanceof L2PcInstance) { pcCount++; if ((((L2PcInstance) obj).getClient() != null) && ((L2PcInstance) obj).getClient().isDetached()) { detachedCount++; } } else if (obj instanceof L2Summon) { summonCount++; } else if (obj instanceof L2DoorInstance) { doorCount++; } else if (obj instanceof L2Character) { charCount++; } } StringBuilder sb = new StringBuilder(); sb.append("Server Status: "); sb.append("\r\n ---> Player Count: " + playerCount + "/" + max); sb.append("\r\n ---> Offline Count: " + detachedCount + "/" + playerCount); sb.append("\r\n +--> Object Count: " + objectCount); sb.append("\r\n +--> AI Count: " + AICount); sb.append("\r\n +.... L2Item(Void): " + itemVoidCount); sb.append("\r\n +.......... L2Item: " + itemCount); sb.append("\r\n +....... L2Monster: " + monsterCount); sb.append("\r\n +......... Minions: " + minionCount); sb.append("\r\n +.. Minions Groups: " + minionsGroupCount); sb.append("\r\n +........... L2Npc: " + npcCount); sb.append("\r\n +............ L2Pc: " + pcCount); sb.append("\r\n +........ L2Summon: " + summonCount); sb.append("\r\n +.......... L2Door: " + doorCount); sb.append("\r\n +.......... L2Char: " + charCount); sb.append("\r\n ---> Ingame Time: " + gameTime()); sb.append("\r\n ---> Server Uptime: " + getUptime(uptime)); sb.append("\r\n ---> GM Count: " + getOnlineGMS()); sb.append("\r\n ---> Threads: " + Thread.activeCount()); sb.append( "\r\n RAM Used: " + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576)); // 1024 * 1024 = 1048576 sb.append("\r\n"); return sb.toString(); }
public void getWebByHomePage() { startTime = System.currentTimeMillis(); this.myDomain = getDomain(); if (this.myDomain == null) { System.out.println("Wrong input!"); return; } System.out.println("Homepage=" + strHomePage); addReport("Homepage=" + strHomePage + "!\n"); System.out.println("Domain=" + myDomain); addReport("Domain=" + myDomain + "!\n"); arrUrl.add(strHomePage); arrUrls.add(strHomePage); allUrls.put(strHomePage, 1); deepUrls.put(strHomePage, 1); File fDir = new File(fPath); if (!fDir.exists()) { fDir.mkdir(); } System.out.println("Start!"); this.addReport("Start!\n"); String tmp = getAUrl(); this.getWebByUrl(tmp, charset, allUrls.get(tmp) + ""); int i = 0; for (i = 0; i < intThreadNum; i++) { new Thread(new Processer(this)).start(); } while (true) { if (arrUrls.isEmpty() && Thread.activeCount() == 1) { long finishTime = System.currentTimeMillis(); long costTime = finishTime - startTime; System.out.println("\n\n\n\nFinished!"); addReport("\n\n\n\nFinished!\n"); System.out.println( "StartTime=" + startTime + "FinishTime=" + finishTime + "CostTime=" + costTime + "ms"); addReport( "StartTime=" + startTime + "FinishTime=" + finishTime + "CostTime=" + costTime + "ms" + "\n"); System.out.println( "Total url number=" + (webSuccessed + webFailed) + "Successed:" + webSuccessed + "Failed:" + webFailed); addReport( "Total url number=" + (webSuccessed + webFailed) + "Successed:" + webSuccessed + "Failed:" + webFailed + "\n"); String strIndex = ""; String tmpUrl = ""; while (!arrUrl.isEmpty()) { tmpUrl = getUrl(); strIndex += "Web depth:" + deepUrls.get(tmpUrl) + " Filepath:" + fPath + "/web" + allUrls.get(tmpUrl) + ".htm" + " url:" + tmpUrl + "\n\n"; } System.out.println(strIndex); try { PrintWriter pwIndex = new PrintWriter(new FileOutputStream("fileindex.txt")); pwIndex.println(strIndex); pwIndex.close(); } catch (Exception e) { System.out.println("��������б�ʧ�ܣ�"); } break; } } }
/** * @param args * @throws Exception */ public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(10); // ThreadPoolExecutor executor = Executors.newFixedThreadPool(10); Callable<Long> worker = new Callable<Long>() { public Long call() throws Exception { long result = 0L; System.out.println("working..."); while (true) { if (Thread.currentThread().isInterrupted()) { System.out.println("canceled...!"); // Thread.currentThread().interrupt(); break; } // Thread.sleep(1000); // simulates work result++; System.out.println("step " + result + "..."); if (result == Long.MAX_VALUE) { break; } } return result; } }; int startCount = Thread.activeCount(); Future<Long> submit1 = executor.submit(worker); Future<Long> submit2 = executor.submit(worker); int middleCount = Thread.activeCount(); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } submit1.cancel(true); submit2.cancel(true); System.out.println("Canceled! " + submit1.isCancelled()); System.out.println("Done! " + submit1.isDone()); try { System.out.println("Get! " + submit1.get()); } catch (CancellationException e1) { // TODO Auto-generated catch block // e1.printStackTrace(); System.out.println("ERROR: " + e1.getMessage()); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } int lastCount = Thread.activeCount(); System.out.println("Count: "); System.out.println(startCount); System.out.println(middleCount); System.out.println(lastCount); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } executor.shutdown(); System.out.println("last count: " + Thread.activeCount()); }
private boolean forwardOrReturnResult( Message msg, RoutingContext[] lastRoutingContexts, final IDAddressPair[][] nextHopCands) { IDAddressPair lastHop = (IDAddressPair) msg.getSource(); int routingID = ((AbstractRecRouteMessage) msg).routingID; final ID[] targets = ((AbstractRecRouteMessage) msg).target; RoutingContext[] routingContexts = ((AbstractRecRouteMessage) msg).cxt; int numResponsibleNodeCands = ((AbstractRecRouteMessage) msg).numRespNodeCands; IDAddressPair initiator = ((AbstractRecRouteMessage) msg).initiator; int ttl = ((AbstractRecRouteMessage) msg).ttl; RoutingHop[] route = ((AbstractRecRouteMessage) msg).route; IDAddressPair[] blackList = ((AbstractRecRouteMessage) msg).blackList; int callbackTag = -1; Serializable[][] callbackArgs = null; if (msg instanceof RecRouteInvokeMessage) { callbackTag = ((RecRouteInvokeMessage) msg).callbackTag; callbackArgs = ((RecRouteInvokeMessage) msg).callbackArgs; } // { // StringBuilder sb = new StringBuilder(); // sb.append("forRetResult called:\n"); // for (ID id: targets) sb.append(" " + id.toString(-1)); // sb.append("\n"); // sb.append(" on " + getSelfIDAddressPair().getAddress() + "\n"); // sb.append(" msg: " + msg.getName() + "\n"); // sb.append(" route:"); // for (RoutingHop h: route) sb.append(" " + h.toString(-1)); // sb.append("\n"); // System.out.print(sb.toString()); // } boolean ttlExpired = false; boolean succeed = true; Message newMsg; boolean[] forwarded = new boolean[targets.length]; for (int i = 0; i < forwarded.length; i++) forwarded[i] = false; Set<IDAddressPair> blackListSet = new HashSet<IDAddressPair>(); if (blackList != null) { for (IDAddressPair a : blackList) { blackListSet.add(a); } } // add this node itself to the resulting route RoutingHop[] lastRoute = route; route = new RoutingHop[lastRoute.length + 1]; System.arraycopy(lastRoute, 0, route, 0, lastRoute.length); route[route.length - 1] = RoutingHop.newInstance(getSelfIDAddressPair()); // TTL check if (ttl < 0) { StringBuilder sb = new StringBuilder(); sb.append("TTL expired (target"); for (ID t : targets) { sb.append(" ").append(t.toString(-1)); } sb.append("):"); for (RoutingHop h : route) { if (h == null) break; sb.append(" "); sb.append(h.getIDAddressPair().toString(-1)); } logger.log(Level.WARNING, sb.toString(), new Throwable()); ttlExpired = true; if (!(msg instanceof RecRouteJoinMessage)) { // allow joining node to succeed succeed = false; } } IDAddressPair[] nextHops = new IDAddressPair[targets.length]; forward: while (true) { if (ttlExpired) break; do { // ... } while (false) Set<IDAddressPair> contactSet = new HashSet<IDAddressPair>(); boolean allContactsAreNull = true; boolean aContactIsNull = false; for (int i = 0; i < targets.length; i++) { if (nextHopCands[i] == null || nextHopCands[i].length <= 0) { nextHops[i] = null; } nextHops[i] = nextHopCands[i][0]; if (nextHops[i] == null) continue; if (blackListSet.contains(nextHops[i].getAddress())) { // next hop is in the black list nextHops[i] = null; System.arraycopy(nextHopCands[i], 1, nextHopCands[i], 0, nextHopCands[i].length - 1); continue; } if (msg instanceof RecRouteJoinMessage && nextHops[i].getAddress().equals(initiator.getAddress())) { // next hop is initiator of routing nextHops[i] = null; System.arraycopy(nextHopCands[i], 1, nextHopCands[i], 0, nextHopCands[i].length - 1); i--; logger.log( Level.WARNING, "Next hop is the joining node " + initiator.getAddress() + ". RoutingAlgorithm#touch() has been called too early?"); continue; } // StringBuilder sb = new StringBuilder(); // sb.append("judge to terminate[" + i + "]: " + targets[i].toString(-1) + "\n"); // sb.append(" on: " + this.getSelfIDAddressPair().toString(-1) + ":\n"); // sb.append(" nextHop: " + nextHops[i].toString(-1) + "\n"); // sb.append(" " + // nextHops[i].getAddress().equals(this.getSelfIDAddressPair().getAddress()) + "\n"); // sb.append(" context: " + (routingContexts[i] != null ? // routingContexts[i].toString(-1) : "null") + "\n"); // sb.append(" lastContext: " + (lastRoutingContexts[i] != null ? // lastRoutingContexts[i].toString(-1) : "null") + "\n"); // if (routingContexts[i] != null && lastRoutingContexts[i] != null) // sb.append(" " + routingContexts[i].equals(lastRoutingContexts[i]) + "\n"); if (nextHops[i].getAddress().equals(this.getSelfIDAddressPair().getAddress()) && (routingContexts[i] == null || routingContexts[i].equals(lastRoutingContexts[i]))) { // next hop is this node itself nextHops[i] = null; // terminates routing // sb.append(" terminate.\n"); } // System.out.print(sb.toString()); if (nextHops[i] != null) { contactSet.add(nextHops[i]); allContactsAreNull = false; } else { contactSet.add(null); aContactIsNull = true; } } if (allContactsAreNull) { // this node is the responsible node break forward; } // fork if (contactSet.size() > 1 || aContactIsNull) { // System.out.println("fork on " + getSelfIDAddressPair().getAddress()); Set<Forwarder> forkedForwarder = new HashSet<Forwarder>(); List<Integer> contactIndexList = new ArrayList<Integer>(); for (IDAddressPair c : contactSet) { contactIndexList.clear(); if (c == null) { for (int i = 0; i < targets.length; i++) if (nextHops[i] == null) contactIndexList.add(i); } else { for (int i = 0; i < targets.length; i++) if (c.equals(nextHops[i])) contactIndexList.add(i); } int nTgts = contactIndexList.size(); final ID[] forkedTarget = new ID[nTgts]; final RoutingContext[] forkedRoutingContext = new RoutingContext[nTgts]; final RoutingContext[] forkedLastRoutingContext = new RoutingContext[nTgts]; final IDAddressPair[][] forkedNextHopCands = new IDAddressPair[nTgts][]; for (int i = 0; i < nTgts; i++) { int index = contactIndexList.get(i); forkedTarget[i] = targets[index]; forkedRoutingContext[i] = routingContexts[index]; forkedLastRoutingContext[i] = lastRoutingContexts[index]; forkedNextHopCands[i] = nextHopCands[index]; } Serializable[][] forkedCallbackArgs = null; if (callbackArgs != null) { forkedCallbackArgs = new Serializable[nTgts][]; for (int i = 0; i < nTgts; i++) { int index = contactIndexList.get(i); forkedCallbackArgs[i] = callbackArgs[index]; } } RoutingHop[] copiedRoute = new RoutingHop[route.length]; System.arraycopy(route, 0, copiedRoute, 0, route.length); Forwarder f = new Forwarder( RecursiveRoutingDriver.getRecRouteMessage( msg.getClass(), routingID, forkedTarget, forkedRoutingContext, numResponsibleNodeCands, initiator, ttl, copiedRoute, blackList, callbackTag, forkedCallbackArgs), forkedLastRoutingContext, forkedNextHopCands); forkedForwarder.add(f); } // execute boolean ret = true; if (config.getUseThreadPool()) { Set<Future<Boolean>> fSet = new HashSet<Future<Boolean>>(); Forwarder firstForwarder = null; ExecutorService ex = SingletonThreadPoolExecutors.getThreadPool( ExecutorBlockingMode.CONCURRENT_REJECTING, Thread.currentThread().isDaemon()); for (Forwarder forwarder : forkedForwarder) { if (firstForwarder == null) { firstForwarder = forwarder; continue; } try { Future<Boolean> f = ex.submit((Callable<Boolean>) forwarder); fSet.add(f); } catch (RejectedExecutionException e) { // invoke directly if rejected // Note that this is required to avoid deadlocks ret &= forwarder.call(); } } ret &= firstForwarder.call(); // direct invocation for (Future<Boolean> f : fSet) { try { ret &= f.get(); } catch (Exception e) { /*ignore*/ } } } else { Set<Thread> tSet = new HashSet<Thread>(); for (Runnable r : forkedForwarder) { Thread t = new Thread(r); t.setName("Forwarder"); t.setDaemon(Thread.currentThread().isDaemon()); tSet.add(t); t.start(); } for (Thread t : tSet) { try { t.join(); } catch (InterruptedException e) { /*ignore*/ } } for (Forwarder f : forkedForwarder) { ret &= f.getResult(); } } return ret; } // if (contactSet.size() > 1 || aContactIsNull) { // fork // System.out.println("forward or reply on " + getSelfIDAddressPair().getAddress()); IDAddressPair nextHop = nextHops[0]; // assert: all nextHops[i].getIDAddressPair() is the same value // System.out.println("On " + getSelfIDAddressPair().getAddress() + ", nextHop: " + // nextHop); // System.out.println(" target: " + target[0]); // prepare a Message newMsg = RecursiveRoutingDriver.getRecRouteMessage( msg.getClass(), routingID, targets, routingContexts, numResponsibleNodeCands, initiator, ttl - 1, route, blackList, callbackTag, callbackArgs); try { Message ack = sender.sendAndReceive(nextHop.getAddress(), newMsg); // throws IOException // System.out.println("On " + getSelfIDAddressPair().getAddress() + ", forwarded " + // Tag.getNameByNumber(oldMsgTag) + " from " + getSelfIDAddressPair().getAddress() + " to // " + nextHop.getAddress()); // fill ID of nextHop for (int i = 0; i < targets.length; i++) { if (nextHops[i].getID() == null) { // this is the case in the first iteration of joining nextHops[i].setID(((IDAddressPair) ack.getSource()).getID()); } } // notify the routing algorithm if (algorithm != null) { algorithm.touch((IDAddressPair) ack.getSource()); } if (ack instanceof RecAckMessage) { for (int i = 0; i < forwarded.length; i++) forwarded[i] = true; break forward; } else { logger.log(Level.SEVERE, "Received message is not REC_ACK."); } } catch (IOException e) { // System.out.println(" failed."); // sending failure and try the next node logger.log( Level.WARNING, "Failed to forward a request to " + nextHop.getAddress() + " on " + getSelfIDAddressPair().getAddress(), e); } // fail to send/receive if (nextHop.getID() != null) { // nextHop.getID() is null when joining super.fail(nextHop); if (blackList != null) { IDAddressPair[] oldBlackList = blackList; blackList = new IDAddressPair[oldBlackList.length + 1]; System.arraycopy(oldBlackList, 0, blackList, 0, oldBlackList.length); } else { blackList = new IDAddressPair[1]; } blackList[blackList.length - 1] = nextHop; blackListSet.add(nextHop); logger.log( Level.INFO, nextHop.getAddress() + " is added to blacklist on " + this.getSelfIDAddressPair().getAddress()); } } while (false); // shift nextHopCands[i] shiftNextHopCands: for (int i = 0; i < targets.length; i++) { if (nextHopCands[i] == null) continue; System.arraycopy(nextHopCands[i], 1, nextHopCands[i], 0, nextHopCands[i].length - 1); for (int j = 0; j < nextHopCands[i].length; j++) { if (nextHopCands[i][j] != null) continue shiftNextHopCands; } nextHopCands[i] = null; } } // forward: while (true) // notify the routing algorithm if (lastHop != null) algorithm.touch(lastHop); // source of message // this is an additional call to touch() compared with iterative lookup if (!this.getSelfIDAddressPair().equals(initiator)) algorithm.touch(initiator); // initiator of message // message dependent processes Serializable[] callbackResult = new Serializable[targets.length]; if (msg instanceof RecRouteInvokeMessage) { // invoke callbacks for (int i = 0; i < targets.length; i++) { callbackResult[i] = invokeCallbacks(targets[i], callbackTag, callbackArgs[i], lastHop, !forwarded[i]); if (callbackResult[i] != null) { logger.log(Level.INFO, "A callback returned non-null object: " + callbackResult[i]); } } } else if (msg instanceof RecRouteJoinMessage) { final IDAddressPair copiedJoiningNode = initiator; final IDAddressPair copiedLastHop = lastHop; final boolean[] copiedForwarded = new boolean[forwarded.length]; System.arraycopy(forwarded, 0, copiedForwarded, 0, copiedForwarded.length); Runnable r = new Runnable() { public void run() { for (int i = 0; i < targets.length; i++) { algorithm.join(copiedJoiningNode, copiedLastHop, !copiedForwarded[i]); } } }; try { if (config.getUseThreadPool()) { ExecutorService ex = SingletonThreadPoolExecutors.getThreadPool( ExecutorBlockingMode.CONCURRENT_NON_BLOCKING, Thread.currentThread().isDaemon()); ex.submit(r); } else { Thread t = new Thread(r); t.setName("Message type specific processes"); t.setDaemon(Thread.currentThread().isDaemon()); t.start(); } } catch (OutOfMemoryError e) { logger.log(Level.SEVERE, "# of threads: " + Thread.activeCount(), e); // Thread[] tarray = new Thread[Thread.activeCount()]; // Thread.enumerate(tarray); // for (Thread t: tarray) System.out.println("Th: " + t.getName()); // System.out.flush(); throw e; } } // reports the routing result to the initiator List<Integer> notForwardedIndexList = new ArrayList<Integer>(); for (int i = 0; i < targets.length; i++) { if (!forwarded[i]) notForwardedIndexList.add(i); } if (!notForwardedIndexList.isEmpty()) { // get candidates for the responsible node ID[] partOfTarget = new ID[notForwardedIndexList.size()]; RoutingResult[] partOfResult = new RoutingResult[notForwardedIndexList.size()]; Serializable[] partOfCallbackResult = new Serializable[notForwardedIndexList.size()]; for (int i = 0; i < notForwardedIndexList.size(); i++) { // target partOfTarget[i] = targets[notForwardedIndexList.get(i)]; // routing result IDAddressPair[] respCands = algorithm.responsibleNodeCandidates(partOfTarget[i], numResponsibleNodeCands); if (msg instanceof RecRouteJoinMessage && initiator.equals(respCands[0])) { // remove initiator from the first place on the responsible node candidates list IDAddressPair[] orig = respCands; respCands = new IDAddressPair[respCands.length - 1]; System.arraycopy(orig, 1, respCands, 0, respCands.length); } partOfResult[i] = new RoutingResult(route, respCands); // callback result partOfCallbackResult[i] = callbackResult[notForwardedIndexList.get(i)]; } // this node is the destination, or failed to send Message repMsg = new RecResultMessage( routingID, succeed, partOfTarget, partOfResult, blackList, partOfCallbackResult); try { sender.send(initiator.getAddress(), repMsg); // System.out.println("replied from " + getSelfIDAddressPair().getAddress() + " to " + // initiator.getAddress() // + " for " + targets[0].toString(-1) + ".."); for (int i : notForwardedIndexList) { forwarded[i] = true; } } catch (IOException e) { // sending failure logger.log( Level.WARNING, "Failed to report to the initiator: " + initiator.getAddress() + " on " + getSelfIDAddressPair().getAddress()); super.fail(initiator); } } // if (!notForwardedIndexList.isEmpty()) boolean ret = true; for (boolean b : forwarded) ret &= b; return ret; }