Java Thread Programming-thread Introduction

Source: Internet
Author: User

Overview

When you browse a webpage, you can read and scroll the text in the webpage while waiting to load the image. Is this a bad way? In the word processing software, when you open another document to be edited, how can you print a document in the background? You may have enjoyed the benefits of replying to an email and downloading large file attachments. By allowing a multi-threadProgramMultiple tasks at the same time can make these convenient functions possible. This book helps you learn these necessary skills and combine this useful feature with your Java program.

What is a thread)

When a modern operating system wants to start running a program, it first creates a process ). A process is a program currently being executed. Each process has at least one running thread ). Sometimes, a thread is considered a lightweight process ). A thread is a programCodeThe execution path (a path of code execution). Each thread has its own local variables, program counters (pointing to commands being executed), and lifecycles. Many modern operating systems allow multiple threads to run simultaneously in one processor. After the JVM (Java Virtual Machine) is started, a new process is created. Many threads can be created in this process.

Generally, the execution of Java code starts from the main () method, and then processes the code along the program path until all the statements in the Main () method are completed. This is an example of a single thread. This "Main" thread is created by JVM, and then the thread starts to execute the main () method, execute all statements in main (), and finally after main () is completed, this thread will die ).

The second thread that is always running in JVM is the garbage collection thread (garbage collection thread ). This thread clears all discarded objects and recycles their memory. Therefore, even a simple Java program that outputs "Hello World" to system. Out is running in a multi-threaded environment. There are two threads, one is the main thread and the other is the garbage collection thread.

When a Java program contains a graphical user interface (GUI), JVM automatically starts multiple threads. One thread is responsible for passing GUI events to corresponding methods in the program, and the other thread is responsible for drawing GUI windows.

For example, a main thread based on a GUI program is responsible for a complex and long-time computing. When the computing is in progress, the user clicks the "Stop computing" button. At this time, the GUI event thread calls the event processing code written for this button to terminate the computing thread. Imagine that if there is only one thread, the two (computing and termination calculation) cannot be performed simultaneously, and it will be difficult to interrupt the calculation.

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.