Advertisement
DulcetAirman

How to start a thread.

Jun 4th, 2013
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  1.     // Step 1: prepare the task as a runnable:
  2.     final Runnable runnable = new Runnable() {
  3.       @Override
  4.       public void run() {
  5.         // ...
  6.       }
  7.     };
  8.     // Or as a Lambda (since Java 8):
  9.     // final Runnable runnable = () -> { ... };
  10.  
  11.     // Step 2: Run it.
  12.     final Thread t = new Thread(runnable);
  13.     // optional: set name, priority etc.
  14.     t.start();
  15.  
  16.  
  17.     // **** OR: *****
  18.  
  19.     final ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
  20.     pool.execute(runnable);
  21.     // Tip: use pool.submit(Callable) to execute a non-void method
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement