Java Learning Lesson 21st (Multithreading (a) Preliminary understanding)

Source: Internet
Author: User

Holiday at home, rest for several days, also did not study, today learn a lot of threads, find feel, the day after tomorrow will go back to school, sad ...

PS: Package does not have any technical content, will be used, the future development must be used, so intend to put a put first, multi-threaded

First, multithreading overview

What is a process?

In layman's terms, it can be understood as an ongoing program in which the process is actually corresponding to an application that belongs to a space in memory.

Process is not directly executed, the process is simply allocating the memory space required by the application, threads responsible for execution, thread responsible for the execution of content in the process of a control unit, also known as the execution path, also known as the execution scenario

What is a thread?

A thread is a control unit (execution path) in the process that is responsible for program execution.

There can be multiple execution paths in a process called multithreading

For example: 360 computer physical examination in the run while the garbage cleaning is also running

At least one thread in a process

Multiple threads are turned on to run multiple parts of the code at the same time

Each thread has its own running content, which can be called a task that the thread will perform


Second, the advantages and disadvantages of multithreading

Seemingly multithreaded technology, efficient, but there are drawbacks.

Windows itself is a multitasking operating system, such as win in the run QQ and 360, seemingly one side in the chat qq,360 side in the scan, they are at the same time, actually not, the CPU is responsible for the execution of the program, in a moment, the CPU is controlling the operation of QQ, At this point 360 will not run, the CPU switching speed is very fast, so feel they are running at the same time, in fact, in a certain moment, only one program in the run, that is, only one path in the execution, so when the application open more time, will compare cards, a single program to get the CPU frequency of execution less (PS : CPU switching is random, dependent on time slice)

Benefit: Solve multi-part simultaneous operation problems

Cons: Too many threads can lead to reduced efficiency

Therefore, although multithreading technology can solve the problem of multi-program operation at the same time, but the program is much more inefficient, or even crash

Of course, even if the CPU is very powerful, but not enough memory space, also do not have fast switching action

Iii. Multithreading in the JVM

The startup of a virtual machine relies on multithreading

The JVM starts with a multi-threaded boot, with at least two threads

1. The thread that executes the main function (main thread)

The thread's task code is defined in the main function

2. Thread responsible for garbage collection (garbage collection Thread)

When the main thread controls the execution of main, there are multiple garbage in the heap memory, and there is another thread to control the garbage collection.

Example |: Heap memory will produce many objects, each object can be recycled, each object is different, have the use of the underlying resources, there is no use, how the object is recycled only the object itself is clear, so each object has a way to be recycled, The method that each object has is defined in the OBJECTT class, called Finalize, that is called by the object's garbage collector when the garbage collector determines that there is no more reference to the object. Subclasses override finalize the method to configure system resources or perform other cleanup.

Class Demo extends Object{public void Finalize () {System.out.println ("demo.finalize");}} public class Main{public static void Main (string[] args) {new demo (); new demo (); System.GC ();//Start garbage collector System.out.println ("Hello word");}}

PS: When the main thread executes to start the garbage collector, the garbage collection thread starts, the main thread continues to execute downward, so the Helloword is printed first, the main thread ends, but the other threads continue to run


Four, the main thread running instance

For heap memory is not a garbage generation will be recycled, because that will compete with the main thread CPU execution, so in a moment who get executive power, who executes


Main thread->ademo.show () into the stack, execution finished stack->bdemo.show () stack, execute the stack->return

If the following code

Class Demo {private String name;public demo (String name) {//TODO auto-generated constructor stubthis.name = name;} public void Show () {for (int i = 0;i<10;i++) {for (int j = -9999999;j<9999999;j++) {}system.out.println (name+ "i =" +i);} }}public class Main{public static void Main (string[] args) {Demo Ademo = new Demo ("A");D emo Bdemo = new Demo ("B"); Ademo.sho W (); Bdemo.show (); System.out.println ("Hello word");}}

This program will be slow, and a does not finish B can not execute, then how to implement a and B simultaneous execution, then you need to implement a in a path, b in a path, the CPU switch, you can achieve A and B running simultaneously

Just like the creation of multithreading.

Java Learning Lesson 21st (Multithreading (a) Preliminary understanding)

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.