Java Multi-thread

Java multi-threading implementation ways mainly contain three:

  • Inherit the “Thread class”
  • Realized the “Runnable interface”
  • ExecutorService, Callable, Future
    • The former two ways have no return value after the thread execution, only the last one is with a return value.

Thread

1
2
3
4
5
public class MyThread extends Thread{
public void run(){
# do something
}
}
1
MyThread myThread = new MyThread();

Runnable

1
2
3
4
5
6
public class MyThread extends OtherClass implements Runnable{
// public class MyThread implements Runnable{
public void run(){
# do something
}
}
1
2
3
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread);
thread.start();

ExecutorService, Callable, Future

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int taskSize = 4;
// init to create a thread pool
ExecutorService pool = Executors.newFixedThreadPool(taskSize);
// create multi-tasks with a return value
List<Future> list = new ArrayList<Future>();
# ...
Callable ca = new MyCallable(taskSize);
// implement the Obj of Future
Future f = pool.submit(c);
list.add(f);
# ...
pool.shutdown();
// obtain the results
for (Future f: list){
f.get().toString();
}

// MyCallable class
class MyCallable implements Callable<Object>{}
  • Statements

// public static ExecutorService newFixedThreadPool(int nThreads)
// create fixed number of threads
// public static ExecutorService newCachedThreadPool()
// * create scheduled pool, which can instead of the Timer class
// public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)