Java multithreading implementation
- Inheriting the Thread
Inherit Thread, rewrite the run method inside, create an instance, and execute start Advantage: Simple code writing Disadvantage: no return value, inherit a class, can not inherit other classes, poor scalabilityCopy the code
- code
public class ThreadDemo extends Thread{
@Override
public void run(a) {
System.out.println("Inherit Thread override run method"+Thread.currentThread().getName()); }}public static void main(String[] args) {
ThreadDemo demo = new ThreadDemo();
demo.setName("llll");
demo.start();
}
Copy the code
- Implement Runnable
Create a Thread class using the Runnable interface implementation object as a parameter to the Thread object, call the Start interface advantage: Thread class can implement multiple interfaces, can inherit a class disadvantages: Without a return value, it cannot be started directly. Instead, it needs to be started by passing the dog in a Thread instanceCopy the code
- code
public class ThreadDemo2 implements Runnable{
@Override
public void run(a) {
System.out.println("Implementing the Runnable interface"); }}public static void main(String[] args) {
ThreadDemo2 demo2 = new ThreadDemo2();
// Thread thread = new Thread(()->{
// system.out.println (" anonymous inner class method ");
/ /});
Runnable target;
Thread thread = new Thread(demo2);
thread.start();
}
Copy the code
- Through Callable and FutureTask
Create the callable interface implementation class, and implement the Call method, combined with FutureTask class to package the Callable object, to achieve multi-threading advantages: has high return value expansion disadvantages: The call method needs to be overridden to combine multiple classes such as FutureTask and ThreadCopy the code
- code
public class MyTask<Object> implements Callable {
@Override
public Object call(a) throws Exception {
System.out.println("Callable"+Thread.currentThread().getName());
return (Object) "Return value"; }}public class main1 {
public static void main(String[] args) {
MyTask myTask = new MyTask();
FutureTask<Object> futureTask = new FutureTask<>(myTask);
Thread thread = new Thread(futureTask);
thread.setName("llll"); thread.start(); }}Copy the code
- Create a thread by creating a thread pool
Custom Runnable interface, implement the RUN method, create a thread pool, call the execution method and pass in the object Advantage: security, high performance, reuse threads disadvantage: jdK5 only support, need to use RunnableCopy the code
public class ThreadDemo4 implements Runnable {
@Override
public void run(a) {
System.out.println(Multithreading via thread pool +runnable, name:+Thread.currentThread().getName()); }}public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(3);
for(int i=0; i<10; i++){ executorService.execute(new ThreadDemo4());
}
System.out.println("Main thread name :"+Thread.currentThread().getName());
// Close the thread pool
executorService.shutdown();
}
Copy the code