Programming basics - Techniques and Tricks III -

Joe

Thành viên VIP
21/1/13
2,701
1,246
113
(cont. Programming basics - Techniques and Tricks II -)

6) Execute or Submit? When you are working with a project that requires a lot of concurrent running threads you have no other choice than to work with a ThreaPool. JAVA Concurrency Package gives you a lot of choices:

- newCachedThreadPool
- newFixedThreadPool
- newScheduledThreadPool
- newWorkStealingPool
- etc.

And the ForkJoinPool. I am not going into details of each "pool" or the differences between them or between Task and Thread, but explain to you what operation is the best for your work: submit or execute? It's a bit confusing between execute and submit.
  • The first as its name says "execute" and it means literally: starts to "execute" the job, while "submit" means putting a plea for an execution and waits a reply (returned value).
  • execute expects a Runnable object while submit takes either a Callable (task) or a Runnable (thread).
  • If you want something running along with the main thread execute is the best choice because submit usually gives back a result (Callable type) and that could block the main thread when the result is waited to complete.
  • A Callable (or task) always returns a value. If nothing is for the return Void is used instead and a "null" must be used as the returned value. And the result can be caught by the API Future<?>.
  • A Runnable is a simplified Thread and won't return anything. Runnable requires less resources (e.g. memory) than Thread, but it could be a problem if you run a Runnable in a Pool and the required memory exceeds the (default Pool) limit your Runnable could hang and you have NO idea whether it's still alive or dead.
PHP:
import java.util.concurrent.*;
public class Test5 {
  public Test5() throws Exception {
    ExecutorService pool = Executors.newWorkStealingPool();
    final long beg1 = System.nanoTime();
    Future<Void> f = pool.submit(() -> {
      long end = System.nanoTime();
      System.out.printf("InsideSubmit TimeDifference %.03f milliSec.\n",((double)(end-beg1)/1000000));
      return null;
    });
    System.out.println("------>"+f.get());
  }
  //
  public static void main(String... A) throws Exception {
    new Test5();
  }
Code:
C:\examples\Test>javac -g:none -d ./classes Test5.java

C:\examples\Test>java Test5
InsideSubmit TimeDifference 1,725 milliSec.
------>null

C:\examples\Test>
If a result is expected, says "long", the submit is as following:
PHP:
import java.util.concurrent.*;
public class Test5 {
  public Test5() throws Exception {
    ExecutorService pool = Executors.newWorkStealingPool();
    final long beg1 = System.nanoTime();
    Future<Long> f = pool.submit(() -> {
      long end = System.nanoTime();
      System.out.printf("InsideSubmit TimeDifference %.03f milliSec.\n",((double)(end-beg1)/1000000));
      return end - beg1;
    });
    System.out.println("------>"+((double)(f.get())/1000000));
  }
  //
  public static void main(String... A) throws Exception {
    new Test5();
  }
Code:
C:\examples\Test>javac -g:none -d ./classes Test5.java

C:\examples\Test>java Test5
InsideSubmit TimeDifference 1,820 milliSec.
------>1.8204
C:\examples\Test>
(cont.)
 
Sửa lần cuối: