1. What is the concurrent and parallel
To learn multithreading, you must first understand what is concurrent and parallel
Parallel: Refers to two or more events at the same time occurs (simultaneously).
Concurrency: refers to two or more events in the same time period occurred within.
2. What is the process, thread
process:
Process is an instance of the program running.
The process is the thread of the container, that is, a process can open multiple threads.
Such as opening a browser, open a word other operations, will create a process.
Thread:
A thread is an independent execution units within a process;
A process can run multiple threads at the same time;
For example, the process can be understood as the hospital, the thread is registration, treatment, payment, medication and other business activities
Multithreading: multiple threads concurrently.
3. Create a thread
Java thread is created, there are four ways:
l Thread class inheritance
l implement Runnable
l realize Callable Interface
l thread pool
3.1. Thread class inheritance
3.1.1 Step 1: Create a custom thread class
com.creatThera Package Penalty for; Import java.util.Date; / * * * @Auther: lanhaifeng * @date: 2019/11/20 0020 09:20 * @Description: inheritance Thread class implements multithreading * @statement: * / public class the MyThread the extends the thread { public void RUN () { for ( int I = 0 ; I < 10 ; I ++ ) { the System. OUT .println ( " MyThread executing thread: " + I); } } public static void main (String [] args) { the MyThread myThread= New new the MyThread (); myThread.start (); for ( int I = 0 ; I < 10 ; I ++ ) { the System. OUT .println ( " main thread is executing: " + I); } } }
Implementation of the results is as follows:
3.2. Implement Runnable
3.2.1 Step 1: Create a custom class that implements the Runnable interface
com.creatThera Package Penalty for; / * * * @Auther: lanhaifeng * @date: 2019/11/20 0020 09:32 * @Description: implement interfaces to create runnable threads * @statement: * / public class MyRunable the implements Runnable { public void RUN () { for ( int I = 0 ; I < 10 ; I ++ ) { the System. OUT .println ( " myRunnable executing thread: " + I); } } // test thread public static void main (String [] args) { the thread thread= New new the Thread ( new new MyRunable ()); Thread.start (); for ( int I = 0 ; I < 10 ; I ++ ) { the System. OUT .println ( " main thread is executing: " + I); } } }
Implementation of the results is as follows:
3.3. Implement Callable Interface
3.3.1. FutureTask Introduction
Callable need help FutureTask class execution, FutureTask class structure is as follows:
Future interfaces:
Determine whether the task is completed: isDone ()
Can interrupt tasks: cancel ()
Task execution results can be obtained: get ()
3.3.2 Step 1: Create a custom class that implements the Callable Interface
com.creatThera Package Penalty for; Import java.util.concurrent. * ; / * * * @Auther: lanhaifeng * @date: 2019/11/20 0020 09:41 * @Description: implement callable interface to create threads * @statement: callable interfaces there is a return value * / public class MyCallnable the implements a Callable <String> { public String Call () throws Exception { for ( int I = 0 ; I < 10 ; I ++ ) { . the System OUT .println ( " myCallable thread is executing: " + I); } return " MyCallabe thread is finished " ; } // test public static void main (String [] args) { // create objects futuretask a FutureTask, <String> = FutureTask new new a FutureTask, <String> ( new new MyCallnable ()); // Create Thread object incoming FutureTask the thread thread = new new the thread (FutureTask); Thread.start (); for ( int I = 0 ; I < 10 ; I ++ ) { the System. OUT .println ( " main thread is executing: "+i); } } }
FIG operating results as follows:
3.4 thread pool -Executor
3.4.1. Thread pool thread class diagram
Executor interface :
Declares execute (Runnable runnable) method, the code to perform tasks
ExecutorService Interface:
Executor interface inheritance, declare method: submit, invokeAll, invokeAny and shutDown etc.
AbstractExecutorService abstract class:
All methods achieve ExecutorService interfaces, basically declared ExecutorService
ScheduledExecutorService接口:
ExecutorService inherited interface method declarations scheduled task
ThreadPoolExecutor类:
Inherited class AbstractExecutorService, achieve execute, submit, shutdown, shutdownNow method
ScheduledThreadPoolExecutor类:
ThreadPoolExecutor class inheritance, and interfaces implemented ScheduledExecutorService implement a method wherein
Executors class:
To provide a method to quickly create a thread pool
3.4.2 Step 1: Create a custom class that implements the Runnable interface
com.creatThera Package Penalty for; Import java.util.concurrent.ExecutorService; Import java.util.concurrent.Executors; / * * * @Auther: lanhaifeng * @date: 2019/11/20 0020 09:52 * @Description: use threads create a thread pool * @statement: * / public class MyExecutors { // implement runnable interface supports only runnable thread pool and Callable class MyRunnable the implements the runnable { public void RUN () { for ( int I = 0 ; I < 10 ; I ++ ) { the System. OUT .println ( "myRunnable thread is executing: " + I); } } } // test create a thread using the thread pool public static void main (String [] args) { // 1. Create a thread pool using Executors ExecutorService ExecutorService = Executors.newFixedThreadPool ( 10 ) ; // 2. thread pool thread of execution, supports only runnable thread pool and Callable executorService.execute ( new new MyRunable ()); // 3. Print the main thread loop for ( int I = 0 ; I < 10 ; I ++ ) { System. OUT.println ( " main main thread is executing: " + I); } } }
Achieve results is as follows:
3.5. Summary
3.5.1. Implement interfaces and inherit the Thread class comparison
l is more suitable for the same interface to multiple threads of program code to share the same resources.
The limitations of single inheritance in java l Interface avoided.
l interface code may be shared by multiple threads, and thread the code independent.
l thread pool threads can only be put in to achieve Runable or Callable interface, not directly into the inheritance Thread class.
expansion:
In java, every time the program starts to run at least two threads. One is the main thread, a garbage collection thread.
3.5.2. Runnable and Callable interfaces compare
Same point:
Both interfaces are l;
L Both can be used to write multithreaded programs;
Both l need to call Thread.start () start the thread;
difference:
l implement Callable interface thread to return an execution result; Runnable interface realized threads can not return results;
l Callable call interface () method throws an exception allowed; without allowing the polishing run Runnable interface () method of the exception;
l realize Callable interface thread can call Future.cancel cancel the execution, and the thread can not implement Runnable
important point:
Callable interface supports the implementation of the results returned, this time need to call FutureTask.get () method implementation, this method blocks until the main thread to obtain 'the future' results; when not call this method, the main thread does not block!