HTML/JavaScript小工具

HTML/JavaScript小工具

2013年5月1日 星期三

ForkJoin-1

要記的單字

 non-async LIFO processing mode.

Which of the following thread pools takes maximum advantage of the available CPUs?
ForkJoinPool

It increases application throughput for CPU intensive tasks.

An advantage of this framework is that it offers a portable means of expressing a parallelizable algorithm without knowing in advance how much parallelism the target system will offer.

The key is to recursively subdivide the task into smaller chunks that can be 
processed by separate threads


You use this “threshold” value in this computation. In other words,


Each worker thread in the Fork/Join framework has a work queue, which is implemented
using a Deque.


The framework is an implementation of the  ExecutorService interface and provides an
easy-to-use concurrent platform in order to exploit multiple processors.



ForkJoinPool is the most important class in the Fork/Join framework. It is a thread pool
for running fork/join tasks—it executes an instance of ForkJoinTask. It executes tasks and
manages their lifecycles.


ForkJoinTask<V> is a lightweight thread-like entity representing a task that defines methods
such as fork() and join().



doRecursiveTask(input) {
        if (the task is small enough to be handled by a thread) {
                compute the small task;
                if there is a result to return, do so
        }
        else {
                divide (i.e., fork) the task into two parts
                call compute() on first task, join() on second task, combine both results and return
        }

smaller pieces recursively.

ForkJoinPoll it uses the number processors available.

(thread number使用 CUP數量)

ForkJoin不適合IO的任務,也不要使用Synchronous

work-stealing.

題目中沒題到newly computed (such as summing them up) 就不需要return
可選用RecursiveAction
 Since there is no requirement to do anything with the newly computed value

that can be broken into smaller pieces recursively.


The worker threads in the ForkJoinPool extend java.lang.Thread and are created by a factory.




class MyCallable implements Callable<String>{
    public String call() throws Exception {
        Thread.sleep(50000);
        return "DONE";
    }
}
public class TestClass {

    public static void main(String[] args) throws Exception {
       ExecutorService es =  Executors.newSingleThreadExecutor();
       Future<String> future = es.submit(new MyCallable());
       System.out.println(future.get()); //1
       es.shutdownNow(); //2
    }
}   


顯示DONE

ForkJoinPool

ForkJoinPool differs from other kinds of ExecutorService mainly by virtue of employing work-stealing
ForkJoinPool pool = new ForkJoinPool(0);必須指定大於0的數
以上會產生錯誤Exception in thread "main" java.lang.IllegalArgumentException

預設設看你的CUP核心多少來確定,通常不要太少效能會比較好
(number of available processors)

The pool attempts to maintain enough active (or available) threads by dynamically adding, suspending, or resuming internal worker threads,

The main forms of these methods accept instances of ForkJoinTask.


getParallelism可由以下方法取得設定值


ForkJoinPool uses a LIFO queue by default executed on that basis

ForkJoin


有两个核心类ForkJoinPool和ForkJoinTask。
ForkJoinPool实现了ExecutorService接口,起到线程池的作用。所以他的用法和Executor框架的使用时一样的,当然Fork Join本身就是Executor框架的扩展。ForkJoinPool有3个关键的方法,来启动线程,execute(…),invoke(…),submit(…)。具体描述如下:
客户端非fork/join调用内部调用fork/join
异步执行execute(ForkJoinTask)ForkJoinTask.fork
等待获取结果invoke(ForkJoinTask)ForkJoinTask.invoke
执行,获取Futruesubmit(ForkJoinTask)ForkJoinTask.fork(ForkJoinTasks are Futures)
ForkJoinTask是分支合并的执行任何,分支合并的业务逻辑使用者可以再继承了这个抽先类之后,在抽象方法exec()中实现。其中exec()的返回结果和ForkJoinPool的执行调用方(execute(…),invoke(…),submit(…)),共同决
下面自己發現低~~~~~~~~~
實驗發現ㄌㄟ!!
execute跟submit這2個東西都怪怪的!!~~在一般狀況下使用都沒反應!
但是~~先執行invoke然後在執行execute或submit才會有動作
有有時執行submit就會有動作!!~~
這是為蝦咪ㄌㄟ!!
程式碼如下:
public  class TestForkJoin {

public static void main(String...strings ){
ForkJoinPool forkJoinPool =
     new ForkJoinPool();
forkJoinPool.execute(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Howard....");
}
});
}
}
不會印出Howard.....

原因!!ForkJoinPool 的執行緒是Daemon 所以不用我們呼叫shdown()他會自動結束
Daemon執行緒的特性在於,當我非Daemon執行緒結束時他就會被close,所以啦以上程式碼
他執行完execute後主執行緒就結束了當然ForkJoinPool 執行緒就停止啦!!~所以不會跑run的內容
只要我們將程式改成!
public  class TestForkJoin {

public static void main(String...strings ){
ForkJoinPool forkJoinPool =
     new ForkJoinPool();
forkJoinPool.execute(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Howard....");
}
});
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最後多個等待,如此執行緒就會運作了!!~~
為蝦咪invoke執行後就可以運行了ㄌㄟ~~
因為invoke有等待的效果!!~~所以嘍!!~~他有停一下跟我們在主執行緒下呼叫sleep效果一樣!!~
想好久才想通~~^^"..~~~原來如此阿!!~~




沒有留言:

張貼留言