1, Processes and threads
1. Process
Process is the basic unit for allocating and scheduling resources (CPU, memory, disk, etc.) by a program. It is an instance of a running program. A program can contain one or more processes.
2. Thread
Thread is the smallest unit that the operating system can calculate and schedule. It is included in the process and is the actual operation unit in the process. A thread refers to a single sequential control flow in a process. Multiple threads can be concurrent in a process, and each thread executes different tasks in parallel.
3. Relationship and difference between process and thread
There can be multiple processes in an operating system, and a process can contain one thread or multiple threads.
Each thread can share the memory in the same process, and the thread also has independent space (stack, program counter).
Threads need less resources and can be regarded as a lightweight process, which makes it more convenient to communicate with each other.
2, How threads are created
Inherit Thread class
Override run method
Create Thread object
Start thread
// Override run method @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + "Thread executed" + i + "second"); } } } public class ThreadDemo { public static void main(String[] args) { // Create Thread object MyThread thread = new MyThread(); // Start thread thread.start(); } }
2. Implement Runnable interface
Implement Runnable interface
Override run method
Create a Thread object and pass in the Runnable object
Start thread
// Implement run method @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + "Thread executed" + i + "second"); } } } public class RunnableDemo{ public static void main(String[] args) { // Create a Thread object and pass in the Runnable object Thread thread = new Thread(new MyRunnable()); // Start thread thread.start(); // Lambda expression creation new Thread(() -> { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + "Thread executed" + i + "second"); } }).start(); } }
3. Implement Callable interface
Implement Callable interface
Create a Callable object and pass in the FutureTask object
Create Thread object, FutureTas object
Start thread
// Implement call method @Override public Long call() throws Exception { long sum = 0; for (int i = 0; i < 1000000000; i++) { sum += i; } return sum; } } public class CallableDemo { public static void main(String[] args) { // Create a Callable object and pass in the FutureTask object FutureTask<Long> task = new FutureTask<>(new MyCallable()); // Create a Thread object and pass in the FutureTask object Thread thread = new Thread(task); // Start thread thread.start(); // Get return value try { System.out.println(task.get()); }catch (Exception e) { e.printStackTrace(); } } }
4. Realize multithreading through thread pool
@Override public void run() { System.out.println("Thread name" + Thread.currentThread().getName()); } } public class ThreadPollDemo { // Number of thread pools private static int threadPoolNum = 10; public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(5); for(int i = 0; i < threadPoolNum; i ++) { //Create thread object MyRunnable thread = new MyRunnable(); executorService.execute(thread); } } }
3, Thread life cycle
1. NEW: when creating an instance of Thread class, the Thread enters the NEW state.
2. RUNNABLE: after calling the start() method of the thread object, wait for the time slice allocated to the CPU, who preempts the CPU resources first, and who starts execution.
3. RUNNING: when the ready thread is called and obtains CPU resources, it enters the RUNNING state and executes the functions in the run() method.
4. BLOCKED: java training When the thread is running, some reasons cause the running thread to enter the blocking state.
5. DEAD: thread execution is completed or forcibly terminated.
4, Common methods of thread