Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between java threads and processes

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the difference between java thread and process". In daily operation, I believe that many people have doubts about the difference between java thread and process. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "what is the difference between java thread and process?" Next, please follow the editor to study!

Process

A process is an execution of a program, and the application runs on the operating system in the form of a process, enjoying the services provided by the operating system. A process is generally composed of program segments, data segments and process control blocks.

Thread

A thread is a sequential execution process of a "process code segment". After the thread evolution is completed, the thread is the smallest unit of CPU scheduling. A process can have one or more threads, and each thread shares the internal space and system resources of the process, and the process is still the smallest unit of operating system resource allocation.

The difference between processes and threads

A process consists of one or more threads

A thread is the smallest unit of CPU scheduling; a process is the smallest unit of resources allocated by the operating system. The division scale of threads is smaller than that of processes, which makes multithreaded programs have high concurrency.

Processes are independent of each other, but the threads within the process share the process's method area memory, heap memory, and system resources.

Thread context switching is faster than process context switching.

Java process

Whenever the java command starts a java application, a JVM process is started. Within this JVM process, all Java program code runs as a thread.

Java main thread

The java program is executed as a thread, and the entry is the main method. Running the main method produces a thread, which is called the main thread. When the main method ends, the main thread runs and the JVM process exits. Three parts: thread description information, program counter, stack memory.

The Java thread corresponds to the os thread one by one. Creating a user thread also requires creating a kernel thread, which is expensive. Thread creation, destruction, switching, the process needs to switch to the kernel state, and then switch back to the user state after completion. The cost of switching between kernel mode and user mode is very high.

The underlying implementation of java thread

It is realized by Win 32 API on windows.

UNIX and Linux adopt the extension of Pthread,POSIX standard and provide user-level or kernel-level libraries.

There are four ways to create a thread

Inherit Thread

Implement Runnable

Create asynchronous tasks with Callable and FutureTask, and then create thread instances. The result can be returned

Take advantage of thread pool

Jdk8's lambda expression (reduce handwriting steps and tell examiners about applications with new jdk8 features)

Thread t = new Thread (()-> System.out.print (Thread.CurrentThread (). GetName () this ends the study of "what's the difference between a Java thread and a process", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report