How to call a method with a separate thread in Java?

let's say I have a doWork() method. How can I call it from a separate thread (and not the main thread).

+99
java multithreading
Aug 15 '10 at 22:30
source share
7 answers

Create a class that implements the Runnable interface. Put the code you want to run in the run() method, the method you must write to fit the Runnable interface. In your main thread, create a new Thread class by passing an instance of your Runnable constructor, and then call start() on it. start tells the JVM to do the magic to create a new thread, and then calls your run method on that new thread.

 public class MyRunnable implements Runnable { private int var; public MyRunnable(int var) { this.var = var; } public void run() { // code in the other thread, can reference "var" variable } } public class MainThreadClass { public static void main(String args[]) { MyRunnable myRunnable = new MyRunnable(10); Thread t = new Thread(myRunnable) t.start(); } } 

Check out the Java concurrency tutorial to get started.

If your method will be called frequently, then it may not be worth creating a new thread every time, as this is an expensive operation. It is probably best to use a thread pool. Check out the Future , Callable , Executor classes in the java.util.concurrent package.

+124
Aug 15 2018-10-18T00:
source share
 Thread t1 = new Thread(new Runnable() { @Override public void run() { // code goes here. } }); t1.start(); 

or

 new Thread(new Runnable() { @Override public void run() { // code goes here. } }).start(); 

or

 new Thread(() -> { // code goes here. }).start(); 

or

 Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { myCustomMethod(); } }); 

or

 Executors.newCachedThreadPool().execute(new Runnable() { @Override public void run() { myCustomMethod(); } }); 
+138
Feb 05 '14 at 6:23
source share

In Java 8, you can do this with a single line of code.

If your method does not accept any parameters, you can use the link to the method:

 new Thread(MyClass::doWork).start(); 

Otherwise, you can call the method in a lambda expression:

 new Thread(() -> doWork(someParam)).start(); 
+51
Jul 28 '15 at 19:53
source share

Another quicker way to call things (like DialogBoxes and MessageBoxes and create separate threads for non-thread safe methods) is to use the Lamba Expression expression

  new Thread(() -> { "code here" }).start(); 
+7
Mar 20 '17 at 14:38
source share

I once wrote a simple utility class that uses the JDK5 executing service and runs certain processes in the background. Since doWork () usually has a void return value, you can use this utility class to execute it in the background.

See in this article where I registered this utility.

+3
Aug 15 '10 at 23:44
source share

To achieve this with RxJava 2.x, you can use:

 Completable.fromAction(this::dowork).subscribeOn(Schedulers.io().subscribe(); 

The subscribeOn() method indicates which scheduler triggers the action: RxJava has several predefined schedulers, including Schedulers.io() , which has a thread pool for I / O, and Schedulers.computation() , which is for CPU intensive operations .

+3
Nov 21 '17 at 22:39
source share

If you use at least Java 8, you can use the runAsync method from the CompletableFuture class

 CompletableFuture.runAsync(() -> {...}); 

If you need to return a result, use supplyAsync instead

 CompletableFuture.supplyAsync(() -> 1); 
0
Jul 23 '19 at 9:24
source share



All Articles