Java's processes and threads

Source: Internet
Author: User

What is a process?

A process is in a running program, it has its own independent memory space, a set of system resources, each process of internal data and state are independent, for example, the window is open more than one Notepad at the same time, although they run the program code is the same, but the use of memory space is independent, non-interference.

What is a thread?

A thread is similar to a process, a piece of code that accomplishes a particular function, a single sequential flow control in a program, but unlike a process, multiple threads of the same class share a chunk of memory space and a set of system resources, and the thread itself usually has only the microprocessor's register data and a stack for the program to execute.

The difference between a process and a thread

Process: Each process has its own code and data space (process context), and the process switching overhead is high.

Threads: A lightweight process that shares code and data spaces with a class of threads, each with a separate run stack and program counter (PC), with minimal thread switching overhead.

Multi-process: In the operating system, you can run multiple task programs at the same time.

Multithreading: In the same application, there are multiple sequential streams executing simultaneously.

Two ways to create threads

Creating Threads with inherited thread classes

This method is relatively simple, mainly by inheriting the Java.lang.Thread class and overriding the thread class's run () method to complete the creation of the line. The thread class is a concrete class, not an abstract class, that encapsulates the behavior of a thread. To create a thread, the programmer must create a new class that is exported from the thread class. The thread class has two of the most important functions run () and start ().

Creating a thread by implementing the Runnable interface

The method generates a class that implements the Java.lang.Runnable interface. This interface only defines a method run (), so it must be implemented in a new class. But the Runnable interface does not support any threads, and we must also create an instance of the thread class by The constructor for the Thread class public thread (Runnable target);


Java's processes and threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.