當前位置: 首頁>>代碼示例>>Java>>正文


Java ExecutorService.isTerminated方法代碼示例

本文整理匯總了Java中java.util.concurrent.ExecutorService.isTerminated方法的典型用法代碼示例。如果您正苦於以下問題:Java ExecutorService.isTerminated方法的具體用法?Java ExecutorService.isTerminated怎麽用?Java ExecutorService.isTerminated使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.concurrent.ExecutorService的用法示例。


在下文中一共展示了ExecutorService.isTerminated方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: shutdownAndCancelTask

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
private static void shutdownAndCancelTask(ExecutorService execService, int shutdownDelaySec, String name, Future future) {
    try {
        execService.shutdown();
        System.out.println("Waiting for " + shutdownDelaySec + " sec before shutting down service...");
        execService.awaitTermination(shutdownDelaySec, TimeUnit.SECONDS);
    } catch (Exception ex) {
        System.out.println("Caught around execService.awaitTermination(): " + ex.getClass().getName());
    } finally {
        if (!execService.isTerminated()) {
            System.out.println("Terminating remaining running tasks...");
            if (future != null && !future.isDone() && !future.isCancelled()) {
                System.out.println("Cancelling task " + name + "...");
                future.cancel(true);
            }
        }
        System.out.println("Calling execService.shutdownNow()...");
        List<Runnable> l = execService.shutdownNow();
        System.out.println(l.size() + " tasks were waiting to be executed. Service stopped.");
    }
}
 
開發者ID:PacktPublishing,項目名稱:Java-9-Cookbook,代碼行數:21,代碼來源:Chapter07Concurrency03.java

示例2: stop

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
static void stop(ExecutorService executor) {
    try {
        System.out.println("attempt to shutdown executor");
        executor.shutdown();
        executor.awaitTermination(5, TimeUnit.SECONDS);
    }
    catch (InterruptedException e) {
        System.err.println("termination interrupted");
    }
    finally {
        if (!executor.isTerminated()) {
            System.err.println("killing non-finished tasks");
        }
        executor.shutdownNow();
        System.out.println("shutdown finished");
    }
}
 
開發者ID:daishicheng,項目名稱:outcomes,代碼行數:18,代碼來源:Executors1.java

示例3: clearData

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
private void clearData(Map<File,DataNode> map,TableMigrateInfo table){
	if(table.isError()) {
		return;
	}
	ExecutorService executor  =  new ThreadPoolExecutor(margs.getThreadCount(), margs.getThreadCount(),
               0L, TimeUnit.MILLISECONDS,
               new LinkedBlockingQueue<Runnable>(),new ThreadPoolExecutor.CallerRunsPolicy());
	Iterator<Entry<File,DataNode>>  it = map.entrySet().iterator();
	while(it.hasNext()){
		Entry<File,DataNode> et = it.next();
		File f =et.getKey();
		DataNode srcDn  =  et.getValue();
		executor.execute(new DataClearRunner(table, srcDn, f));
	}
	executor.shutdown();
	while(true){
		if(executor.isTerminated()){
			break;
		}
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			LOGGER.error("error",e);
		}
	}
}
 
開發者ID:huang-up,項目名稱:mycat-src-1.6.1-RELEASE,代碼行數:27,代碼來源:DataMigrator.java

示例4: awaitExecutorTermination

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * Wait for specified executor to complete
 * @param executor
 * @param timeoutMillis Number of milliseconds to wait until forcibly shutting down
 */
public static void awaitExecutorTermination(ExecutorService executor, long timeoutMillis) {
    try {
        // wait for all executors to complete
        executor.awaitTermination(timeoutMillis, TimeUnit.MILLISECONDS);

    } catch (InterruptedException e) {
        // nothing to do, shutting down

    } finally {
        // shutdown all running processes
        if (!executor.isTerminated()) {
            logger.warn("Forcing shutdown for {}", executor);
            executor.shutdownNow();
        }
    }
}
 
開發者ID:salesforce,項目名稱:pyplyn,代碼行數:22,代碼來源:ShutdownHook.java

示例5: insert

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
private void insert(String insertType) throws InterruptedException {
	this.logger.write("Start to insert" + insertType + ".");
	Iterator<Inserter> iter = this.factory.inserters(insertType);
	ExecutorService pool = Executors.newFixedThreadPool(this.threads);
	while (iter.hasNext())
		pool.execute(iter.next());
	pool.shutdown();
	while (true) {
		if (pool.isTerminated()) {
			this.logger.write("Finish " + insertType + " insert.");
			break;
		}
		Thread.sleep(20);
	}

}
 
開發者ID:bigbai0210,項目名稱:Oracle2Neo4j,代碼行數:17,代碼來源:Controller.java

示例6: testWrite_MultipleWriters

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
   * Test for multiple writers running in separate threads, writing data simultaneously.
   *
   * @throws RMIException
   * @throws IOException
   * @throws InterruptedException
   */
  @Test
  public void testWrite_MultipleWriters() throws RMIException, IOException, InterruptedException {
//    System.out.println("MonarchRecordWriterTest.testWrite_MultipleWriters");
    final List<String> lines = TestHelper.getResourceAsString(resourceFile);
    final Map<String, Integer> expectedMap = new HashMap<String, Integer>(4) {{
      put("0", 10);
      put("1", 10);
      put("2", 10);
      put("3", 1);
    }};
    final int blockSize = 10;

    ExecutorService es = Executors.newFixedThreadPool(3);
    for (int i = 0; i < 3; i++) {
      final String tid = "000_" + i;
      es.submit(() -> {
        try {
          assertOnRecordWriter(lines, expectedMap, tid, blockSize);
        } catch (Exception e) {
          fail("No exception expected: " + e.getMessage());
        }
      });
    }
    es.shutdown();
    es.awaitTermination(5, TimeUnit.SECONDS);
    if (!es.isTerminated()) {
      es.shutdownNow();
    }
    Configuration conf = new Configuration();
    conf.set("monarch.locator.port", testBase.getLocatorPort());
  }
 
開發者ID:ampool,項目名稱:monarch,代碼行數:39,代碼來源:MonarchRecordWriterFTableTest.java

示例7: stop

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
public static void stop(ExecutorService executor) {
    try {
        executor.shutdown();
        executor.awaitTermination(60, TimeUnit.SECONDS);
    }
    catch (InterruptedException e) {
        System.err.println("termination interrupted");
    }
    finally {
        if (!executor.isTerminated()) {
            System.err.println("killing non-finished tasks");
        }
        executor.shutdownNow();
    }
}
 
開發者ID:daishicheng,項目名稱:outcomes,代碼行數:16,代碼來源:ConcurrentUtil.java

示例8: main

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
public static void main(String[] args)
{
	ExecutorService executor = Executors.newFixedThreadPool(5);
	for (int i = 0; i < 10; i++)
	{
		Runnable worker = new WorkerThread("" + i);
		executor.execute(worker);
	}
	executor.shutdown();
	while (!executor.isTerminated())
	{
	}
	System.out.println("Finished all threads");
}
 
開發者ID:baohongfei,項目名稱:think-in-java,代碼行數:15,代碼來源:SimpleThreadPool.java

示例9: shutdownExecutor

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
public static void shutdownExecutor(ExecutorService executor) {
    executor.shutdown();
    try {
        executor.awaitTermination(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    if (! executor.isTerminated()) {
        Logger.logShutdownMessage("some threads didn't terminate, forcing shutdown");
        executor.shutdownNow();
    }
}
 
開發者ID:muhatzg,項目名稱:burstcoin,代碼行數:13,代碼來源:ThreadPool.java

示例10: executeSameTimeJob

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * 並行任務處理方式
 */
private boolean executeSameTimeJob(final long groupId, final Queue<JobVo> jobQueue) {
    final List<Boolean> result = new ArrayList<Boolean>(0);

    final Semaphore semaphore = new Semaphore(jobQueue.size());
    ExecutorService exec = Executors.newCachedThreadPool();

    for (final JobVo jobVo : jobQueue) {
        Runnable task = new Runnable() {
            @Override
            public void run() {
                try {
                    semaphore.acquire();
                    result.add(doFlowJob(jobVo, groupId));
                    semaphore.release();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        exec.submit(task);
    }
    exec.shutdown();
    while (true) {
        if (exec.isTerminated()) {
            logger.info("[opencron]SameTimeJob,All doned!");
            return !result.contains(false);
        }
    }
}
 
開發者ID:wolfboys,項目名稱:opencron,代碼行數:33,代碼來源:ExecuteService.java

示例11: scanportsetfun

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
public void scanportsetfun() {
   	 int threadNum;
        threadNum=Integer.parseInt(TheardNum.getText().trim());
        String[] portsString = textportlist.getText().split(",");
        
        int[] ports = new int[portsString.length];
        
        for(int ii=0;ii<portsString.length;ii++)
            ports[ii] = Integer.parseInt(portsString[ii].trim());
        
        Result.append("scanning"+" "+Lsaportscan.hostaddress+"'s port set!"+"\n");
        ExecutorService threadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < threadNum; i++) {
        	 Portset portset = new Portset(ports,threadNum,i);
         	 threadPool.execute(portset);
        }
        
        threadPool.shutdown();
        
        while (true) {
         	
            if (threadPool.isTerminated()) {
                //System.out.println("OVER!!!!!!!!!!!!!!!");
                Result.append("-------------------------Over!!!----------------------\n\n");
                stateResult.setText("OVER !");  
                break;
            }
        
            try {
               		Thread.sleep(1000);
            } catch (InterruptedException e1) {
        }  
    }
}
 
開發者ID:theLSA,項目名稱:lsascan_v1.0_linux,代碼行數:35,代碼來源:Lsascan_v1_frame.java

示例12: multiThreadedPersist

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
@Test
public void multiThreadedPersist() throws RepositoryException, InterruptedException {
    final PersistenceService persistenceService = new PersistenceService(host, port, adminUser, adminPassword);

    //persist data with multiple threads against the persistence service - simulate multiple concurrent requests against a tomcat deployed ingestion service
    //results in intermittent MarkLogicTransactionExceptions in executor threads
    ExecutorService executorService = Executors.newFixedThreadPool(10);
    try {
        for(final String identifier: identifiers) {
            for (int i=0; i<20; i++) {
                executorService.submit(
                        new Runnable() {
                            @Override
                            public void run() {
                                persistenceService.persist(entitiesFor(identifier));
                            }
                        }
                );
            }
        }

        executorService.shutdown();
        executorService.awaitTermination(60, TimeUnit.SECONDS);
    } finally {
        if(!executorService.isTerminated()) {
            System.out.println("cancel non finished tasks");
        }
        executorService.shutdownNow();
        System.out.println("shut down finished");
    }
}
 
開發者ID:marklogic,項目名稱:marklogic-rdf4j,代碼行數:32,代碼來源:MultiThreadedPersistenceTest.java

示例13: test_multi

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
@Test
public void test_multi() {

    // 使用ConcurrentHashMap代替HashSet(內部使用HashMap,不適用於並發),以應用於並發條件下
    final ConcurrentMap<Singleton2 ,Boolean> map = new ConcurrentHashMap<>() ;
    ExecutorService exec = Executors.newFixedThreadPool(200) ;

    // 約定一個執行時間(模擬並發,原因是類比較簡單,初始化速度太快,無法真實再現並發情況)
    final long moment = System.currentTimeMillis() + 1000 ;
    for (int i = 0; i < 1000; i++) {
        exec.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    // 到約定時間後再執行(構成並發)
                    if (moment < System.currentTimeMillis()) {
                        map.put(Singleton2.getInstance() ,Boolean.TRUE) ;
                        break;
                    }
                }
            }
        });
    }

    exec.shutdown();
    while (!exec.isTerminated()) ;

    // 如果元素個數為1,表示未生成不同實例
    Assert.assertTrue(map.size() > 1);
}
 
開發者ID:zlikun,項目名稱:zlikun-jee-design-pattern,代碼行數:31,代碼來源:Singleton2Test.java

示例14: test_multi

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
@Test
public void test_multi() {

    // 使用ConcurrentHashMap代替HashSet(內部使用HashMap,不適用於並發),以應用於並發條件下
    final ConcurrentMap<Singleton6 ,Boolean> map = new ConcurrentHashMap<>() ;
    ExecutorService exec = Executors.newFixedThreadPool(400) ;

    // 約定一個執行時間(模擬並發,原因是類比較簡單,初始化速度太快,無法真實再現並發情況)
    final long moment = System.currentTimeMillis() + 1000 ;
    for (int i = 0; i < 1000; i++) {
        exec.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    // 到約定時間後再執行(構成並發)
                    if (moment < System.currentTimeMillis()) {
                        map.put(Singleton6.INSTANCE ,Boolean.TRUE) ;
                        break;
                    }
                }
            }
        });
    }

    exec.shutdown();
    while (!exec.isTerminated()) ;

    // 如果元素個數為1,表示未生成不同實例
    Assert.assertEquals(1 ,map.size());
}
 
開發者ID:zlikun,項目名稱:zlikun-jee-design-pattern,代碼行數:31,代碼來源:Singleton6Test.java

示例15: test_multi

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
@Test
public void test_multi() {

    // 使用ConcurrentHashMap代替HashSet(內部使用HashMap,不適用於並發),以應用於並發條件下
    final ConcurrentMap<Singleton4 ,Boolean> map = new ConcurrentHashMap<>() ;
    ExecutorService exec = Executors.newFixedThreadPool(400) ;

    // 約定一個執行時間(模擬並發,原因是類比較簡單,初始化速度太快,無法真實再現並發情況)
    final long moment = System.currentTimeMillis() + 1000 ;
    for (int i = 0; i < 1000; i++) {
        exec.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    // 到約定時間後再執行(構成並發)
                    if (moment < System.currentTimeMillis()) {
                        map.put(Singleton4.getInstance() ,Boolean.TRUE) ;
                        break;
                    }
                }
            }
        });
    }

    exec.shutdown();
    while (!exec.isTerminated()) ;

    // 如果元素個數為1,表示未生成不同實例
    Assert.assertEquals(1 ,map.size());
}
 
開發者ID:zlikun,項目名稱:zlikun-jee-design-pattern,代碼行數:31,代碼來源:Singleton4Test.java


注:本文中的java.util.concurrent.ExecutorService.isTerminated方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。