本文整理汇总了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");
}
示例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());
}
示例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");
}
示例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);
}
示例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");
}
示例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");
}