0

ExecutorService は初めてです。現在、私のシナリオは「365*24*7 の数百万のデータが入ってくる」というもので、スレッドを使用して入ってくるデータに対して処理を行う必要があります。

ExecutorService es = Executors.newSingleThread();
es.execute(new ComputeDTask(data));

実行のために ComputeDTask にデータを送信しています。

new ComputeDTaskデータが入るたびに を作成するのはどれくらい効率的ですか? つまり、データを 100 万回受信すると、100 万個の ComputeDTask オブジェクトが作成されます。

4

3 に答える 3

2

The overhead of creating a thread is about 100 micro-seconds. i.e. if you do less than 100 micro-seconds of work you will have more overhead than work done and you program can be slower that being single threaded.

The overhead of create a task to an existing Executor service is about 2 micro-seconds. i.e. if the task takes less than 2 micro-second you may have more overhead than real work done.

If you have CPU bound process, you need about the same number of threads as core to keep all the cores busy, while minimising overhead.

e.g. if you have 8 cores, I suggest you combine the work done so you have 8 threads with one task each total. You can have more tasks than this but you may find it take longer to process.

Of course you should shutdown your ExecutorService when you have finished with it. The reason you don't see this done in all examples is that it can be a good idea to create one ExecutorService which is used for the life of the application.

于 2012-07-25T10:04:13.110 に答える
1

Apparently you are creating a whole new ExecutorService for each task and never shutting them down. This of course results in the thread leak that you are observing. The proper way to use the ExecutorService is to create a single instance that manages the thread pool for you. The executors are very flexible and powerful in the way they manage the threads.

于 2012-07-25T10:03:52.477 に答える
0

着信データでは、を使用してタスクを作成し、new ComputeDTask(data)これをThreadPoolに渡します。ここで100スレッドとすると、より高いスループットでタスクを実行できます。

ExecutorService es = Executors.newFixedThreadPool(100);

onGetData(){

     es.execute(new ComputeDTask(data));

 }
于 2012-07-25T10:13:05.127 に答える