Implementation of threads and their respective advantages and disadvantages & lock than synchronized advantage

One. Thread class inheritance

  Thread class itself is an instance implement Runnable, a representative example of a thread. The only way to start the thread is () instance method by start Thread class. start () method is a local method, he will start a new thread, and executes run () method.

To do: through the direct successor Thread class created their own, rewrite the run () method, you can start a new thread and customize the run () method.

 

The code is simple, the class can not inherit from other classes.

Second, implement Runnable

A class can implement more than, but not multiple inheritance.

Inherit from other classes, implement the unified interface instance can share resources.

Third, to achieve Callable Interface

A class can implement more than, but not multiple inheritance.

call Callable interface () method returns a value, Runnable not;

Inherit from other classes, implement the unified interface instance can share resources.

Fourth, the thread pool mode

A container accommodating a plurality of threads, wherein the threads can be reused, often eliminating the need for creating a thread objects. Automated assembly, recycling of resources.

 

 

Five, lock than synchronized advantage

Formula 1 now acquire and release locks.

2 facilitate the achievement of fair locks.

 

Guess you like

Origin www.cnblogs.com/fengtangjiang/p/11100417.html