Process and thread
Relationship between process and thread
- process
Process is a running activity of a computer program on a data set. It is the basic unit for resource allocation and scheduling of the system and the basis of the structure of the operating system. In the contemporary computer architecture of thread oriented design, process is the container of thread. Program is the description of instructions, data and their organizational form, and process is the entity of program. - thread
Thread is the smallest unit that the operating system can schedule operations. 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.
Differences between processes and threads
- Thread is the smallest unit of program execution, and process is the smallest unit of resource allocation by the operating system;
- A process consists of one or more threads. Threads are different execution routes of code in a process
- Processes are independent of each other, but each thread in the same process shares the memory space of the program (including code segments, data sets, heaps, etc.) and some process level resources (such as opening files and signals, etc.), and the threads in a process are not visible in other processes;
- Scheduling and switching: thread context switching is much faster than process context switching
Summary:
- Process: refers to an application running in the system; Once a program runs, it is a process; Process - the smallest unit of resource allocation.
- Thread: the basic unit in which the system allocates processor time resources, or a unit execution flow executed independently within a process. Thread -- the smallest unit of program execution.
Concurrency and parallelism
Concurrent programming: parallelism and concurrency
- Concurrent
- The concurrency graph only executes any one of three threads A, B and C at the same time in the micro sense, while in the macro sense, it is the illusion that three threads execute "at the same time".
- Multithreading realizes concurrency. Threads will compete for CPU resources and execution opportunities. Threads will grab as many time slices as possible for themselves.
- The essence of concurrency is that a physical CPU (or multiple physical CPUs) is multiplexed among several threads. Threads get execution opportunities through competition. Threads are similar to alternating execution.
- parallel
- The three threads A, B and C in the parallel graph execute simultaneously on the multi-core CPU, and are really executed simultaneously at the same time in the micro and macro.
- Parallelism means that two or more threads occur at the same time and execute simultaneously on multi-core CPU s. There is no concept of competition and waiting
The essence of concurrent programming: make full use of CPU resources. Whether concurrent or parallel, it improves the utilization of CPU resources and makes maximum use of CPU resources.
Thread creation method
Three thread creation methods
- Inherit Thread class
public class TestThread extends Thread { @Override public void run() { for (int j = 0; j< 300; j++) { System.out.println("Sub thread print number--->" + j); } } public static void main(String[] args) { // Start another thread TestThread thread = new TestThread(); // Call the start() method thread.start(); for (int i = 0; i < 300; i++) { System.out.println("main Thread print number--->" + i); } } }
- Implement Runnable interface
public class RunDemo implements Runnable { @Override public void run() { for (int j = 0; j < 300; j++) { System.out.println("Sub thread print number--->" + j); } } public static void main(String[] args) { RunDemo runDemo = new RunDemo(); // The class that implements the Runnable interface is used as the constructor parameter of the Thread class Thread thread = new Thread(runDemo); thread.start(); for (int i = 0; i < 300; i++) { System.out.println("main Thread print number--->" + i); } } }
- Implement Callable interface
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.concurrent.*; public class CallDemo implements Callable<Boolean> { private String downFilePath; private String fileName; public CallDemo(String downFilePath, String fileName) { this.downFilePath = downFilePath; this.fileName = fileName; } @Override public Boolean call() throws Exception { DownFile downFile = new DownFile(); downFile.downFile(downFilePath, fileName); System.out.println("The downloaded picture file name is: " + fileName); return true; } public static void main(String[] args) throws ExecutionException, InterruptedException { // Multi thread downloading pictures CallDemo thread1 = new CallDemo("http://pics6.baidu.com/feed/0df431adcbef76094eb4e2500aa45ec47ed99ec7.jpeg?token=3fd538515284ed1b44f8681639534006", "1.jpeg"); CallDemo thread2 = new CallDemo("http://pics7.baidu.com/feed/6c224f4a20a4462326cb7ba0bc5b8f060df3d775.jpeg?token=1a2595cd0b4dd822e3b8e79438361e80", "2.jpeg"); CallDemo thread3 = new CallDemo("http://pics7.baidu.com/feed/6c224f4a20a4462326cb7ba0bc5b8f060df3d775.jpeg?token=1a2595cd0b4dd822e3b8e79438361e80", "3.jpeg"); // Start multiple threads ExecutorService executorService = Executors.newFixedThreadPool(3); // Execution thread Future<Boolean> future1 = executorService.submit(thread1); Future<Boolean> future2 = executorService.submit(thread2); Future<Boolean> future3 = executorService.submit(thread3); // View return value System.out.println(future1.get()); System.out.println(future2.get()); System.out.println(future3.get()); } class DownFile{ public void downFile(String downFilePath, String fileName) { try { FileUtils.copyURLToFile(new URL(downFilePath), new File(fileName)); } catch (IOException e) { e.printStackTrace(); } } } }
ending:
I hope you can gain something after reading this article. At the same time, if this article is incorrect, please also point out that we can learn and make progress together.
With this poem, I encourage you: no small steps, no even thousands of miles; If you don't accumulate small streams, you can't form rivers and seas.