Multithreading three ways to achieve their advantages and disadvantages

There are many ways to achieve multi-threaded, common are the following three:

1, the Thread class inheritance, override run () method.

1) define a subclass of Thread class, the class and override run () method, the method body of the run () method represents the thread to complete the task. Thus the run () method is called executable.

2) create an instance of a subclass of Thread is to create a thread object.

3) call the thread object's start () method to start a thread.

2, implement Runnable, override run () method.

1) implement class Runnable interface, and run the rewriting process () method, the run () method of the same thread is executable.

2) Create an instance of Runnable implementation class, and so as an example of a target to create Thread Thread object, the Thread object is the real thread object.

3) call the thread object's start () method to start a thread.

3, the thread is achieved by implementing the interface and use FutureTask Callable Wrapper.

1) Creating Callable interface implementation class, and implements call () method, the method body call () method of execution is also a body of the thread.

2) Creates an instance of the implementation class Callable using FutureTask packaging Callable object class, the object encapsulates FutureTask Callable object of the call () Returns the value of the method.

3) Use FutureTask objects to create and start a new thread as the target Thread object.

4) call FutureTask object get () method to obtain the return value after the end of the sub-thread execution.

Three kinds of comparative advantages and disadvantages of implementation:

1, to achieve Runnable and Callable Interface:

advantage:

1) Thread class implements Runnable interface only (JDK1.0 start) or Callable Interface (JDK1.5 start), it can also inherit from other classes.

2) multiple threads can share the same target object is identical for a plurality of threads to deal with a case where the same resources, which can be separated from the CPU, and the code data, to form a clear model better reflects the object-oriented thinking.

3) Callable interface to create multiple threads to achieve the greatest benefit is that you can have a return value.

Disadvantages:

Programming slightly complicated, if you want to access the current thread, you must use Thread.currentThread () method.

2, the use of inheritance Thread class method:

advantage:

Write simple, if you want to access the current thread without the use of Thread.currentThread () method, the direct use this to get the current thread.

Disadvantages:

Thread class Thread class has inherited, can not be inherited (single inheritance in java) other classes, so this is not flexible.

Additional information:

1) Callable predetermined override call () method; override the Runnable run () method.

2) after the completion of the task may be performed Callable return value; the Runnable task is to not have a return value.

3) call () method can throw an exception; RUN () methods can not.

4) Run the task can get a Future Callable object that represents the result of the asynchronous computation. It provides a method of checking computation is complete, to wait for the completion of calculation, and check the result of the calculation. By Future object can understand the implementation of the mandate, to cancel the execution of the task, you can get the results.

Guess you like

Origin www.cnblogs.com/sinoaccer/p/12116469.html