当前位置: 首页>>代码示例>>Java>>正文


Java ForkJoinPool.awaitTermination方法代码示例

本文整理汇总了Java中java.util.concurrent.ForkJoinPool.awaitTermination方法的典型用法代码示例。如果您正苦于以下问题:Java ForkJoinPool.awaitTermination方法的具体用法?Java ForkJoinPool.awaitTermination怎么用?Java ForkJoinPool.awaitTermination使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.ForkJoinPool的用法示例。


在下文中一共展示了ForkJoinPool.awaitTermination方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
 * Main method of the example
 * @param args
 */
public static void main(String[] args) {

	// Generate an array of 1000 integers
	ArrayGenerator generator=new ArrayGenerator();
	int array[]=generator.generateArray(1000);
	
	// Create a TaskManager object
	TaskManager manager=new TaskManager();
	
	// Create a ForkJoinPool with the default constructor
	ForkJoinPool pool=new ForkJoinPool();
	
	// Create a Task to process the array
	SearchNumberTask task=new SearchNumberTask(array,0,1000,5,manager);
	
	// Execute the task
	pool.execute(task);

	// Shutdown the pool
	pool.shutdown();
	
	
	// Wait for the finalization of the task
	try {
		pool.awaitTermination(1, TimeUnit.DAYS);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	
	// Write a message to indicate the end of the program
	System.out.printf("Main: The program has finished\n");
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:37,代码来源:Main.java

示例2: main

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
 * Main method of the class
 */
public static void main(String[] args) {
	// Array of 100 integers
	int array[]=new int[100];
	// Task to process the array
	Task task=new Task(array,0,100);
	// ForkJoinPool to execute the Task
	ForkJoinPool pool=new ForkJoinPool();
	
	// Execute the task
	pool.execute(task);

	// Shutdown the ForkJoinPool
	pool.shutdown();
	
	// Wait for the finalization of the task
	try {
		pool.awaitTermination(1, TimeUnit.DAYS);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	
	// Check if the task has thrown an Exception. If it's the case, write it
	// to the console
	
	if (task.isCompletedAbnormally()) {
		System.out.printf("Main: An exception has ocurred\n");
		System.out.printf("Main: %s\n",task.getException());
	}
	
	System.out.printf("Main: Result: %d",task.join());
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:35,代码来源:Main.java

示例3: main

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
 * Main method of the example
 * @param args
 */
public static void main(String[] args) {

	// Creates a task
	OneSecondLongTask task=new OneSecondLongTask();
	
	// Creates a new Handler
	Handler handler = new Handler();
	// Creates a Factory
	AlwaysThrowsExceptionWorkerThreadFactory factory=new AlwaysThrowsExceptionWorkerThreadFactory();
	// Creates a new ForkJoinPool
	ForkJoinPool pool=new ForkJoinPool(2,factory,handler,false);
	
	// Execute the task in the pool
	pool.execute(task);

	// Shutdown the pool
	pool.shutdown();
	
	// Wait for the finalization of the tasks
	try {
		pool.awaitTermination(1, TimeUnit.DAYS);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	
	System.out.printf("Main: The program has finished.\n");
	
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:33,代码来源:Main.java

示例4: pool

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
static void pool() throws Exception {
    System.out.println("pool()");
    ForkJoinPool fjPool = new ForkJoinPool(2);
    fjPool.submit(() -> {
        int result = IntStream.range(0, 5)
                .parallel()
                .peek(it -> System.out.printf("Thread [%s] peek: %d\n", Thread.currentThread().getName(), it))
                .sum();
        System.out.println("sum: " + result);
    });
    fjPool.awaitTermination(1, TimeUnit.SECONDS);
}
 
开发者ID:vitaly-chibrikov,项目名称:otus_java_2017_10,代码行数:13,代码来源:Parallel.java

示例5: main

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
 * @param args
 */
public static void main(String[] args) throws Exception {

	/*
	 * Create the Fork/Join pool
	 */
	ForkJoinPool pool=new ForkJoinPool();
	
	/*
	 * Create a new Array of integer numbers with 10000 elements
	 */
	int array[]=new int[10000];
	
	/*
	 * Create a new task
	 */
	Task task1=new Task(array,0,array.length);
	
	/*
	 * Execute the task in the Fork/Join pool
	 */
	pool.execute(task1);
	
	/*
	 * Wait for the finalization of the task writing information
	 * about the pool every second
	 */
	while (!task1.isDone()) {
		showLog(pool);
		TimeUnit.SECONDS.sleep(1);
	}
	
	/*
	 * Shutdown the pool
	 */
	pool.shutdown();
	
	/*
	 * Wait for the finalization of the pool
	 */
	pool.awaitTermination(1, TimeUnit.DAYS);
	
	/*
	 * End of the program
	 */
	showLog(pool);
	System.out.printf("Main: End of the program.\n");

}
 
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:52,代码来源:Main.java

示例6: main

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
 * @param args
 */
public static void main(String[] args) throws Exception {

	/*
	 * Create a new MyWorkerThreadFactory
	 */
	MyWorkerThreadFactory factory=new MyWorkerThreadFactory();
	
	/*
	 * Create new ForkJoinPool, passing as parameter the factory created before
	 */
	ForkJoinPool pool=new ForkJoinPool(4, factory, null, false);
	
	/*
	 * Create and initializes the elements of the array
	 */
	int array[]=new int[100000];
	
	for (int i=0; i<array.length; i++){
		array[i]=1;
	}
	
	/*
	 * Create a new Task to sum the elements of the array
	 */
	MyRecursiveTask task=new MyRecursiveTask(array,0,array.length);
	
	/*
	 * Send the task to the ForkJoinPool 
	 */
	pool.execute(task);
	
	
	/*
	 * Wait for the finalization of the task
	 */
	task.join();
	
	/*
	 * Shutdown the pool
	 */
	pool.shutdown();
	
	/*
	 * Wait for the finalization of the pool
	 */
	pool.awaitTermination(1, TimeUnit.DAYS);
	
	/*
	 * Write the result of the task
	 */
	System.out.printf("Main: Result: %d\n",task.get());
	
	/*
	 * Write a message indicating the end of the program
	 */
	System.out.printf("Main: End of the program\n");
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:61,代码来源:Main.java


注:本文中的java.util.concurrent.ForkJoinPool.awaitTermination方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。