Java Threading Understanding 001

Source: Internet
Author: User

Concepts: Threads and processes

Process: Simply put, a process is an application, that is, a program. Today's operating systems are generally multi-process operating systems. For example: You can open the music player at the same time, you can also open the browser and video player.

Threads: threads, which exist in a process, are understood to have multiple threads in a process. The current program is also mostly multithreaded programs. For example, a music player can play music at the same time, or it can download music at the same time.

Single-threaded and multithreaded execution order:

Single-threaded execution: The thread executes progressively downward and eventually ends.

Multi-threaded program execution: Fast switching between two threads (grabbing CPU resources), getting CPU resources to execute downward

Note: Multithreading is essentially a single-threaded programming, just because the CPU switch quickly and quickly, give us the illusion that two tasks at the same time.

Thread execution Process:

1. instantiate a thread object and call the start () method, and the thread enters a ready state (which can go to preempt CPU resources).

2. when the CPU resource is acquired, enter the Running State (execution state).

3.Running Status, you may experience other conditions (such as no resources when accessing the network), such as blocking the Blocked state when the blocking is in the ready state.

4. when the Run () method finishes executing, the thread dies Dead;

It is inferred from the running of the thread that the main method of threading is written in the run() method, also called the Run () method body as the thread body.

Example Analysis:

To test the class main function:

 Packageinterthread_v001; Public classtest_v001{ Public Static voidMain (String args[]) {//instantiating a Thread objectthread_v001 TV=Newthread_v001 (); //start Thread--go to Ready stateTv.start (); //Tv.run ();          for(inti = 0; I < 100; i++) {System.out.println ("Mainthread-->" +i); }}}//Thread class: (Overrides the Run () method) Packageinterthread_v001; Public classthread_v001extendsthread{//inherit the thread class provided by the JDK, overriding the Run () method     Public voidrun () { for(inti = 0; I < 100;i++) {System.out.println ("Thread_v001-->" +i); }    }} 

Execution Result:

Analysis: It can be seen from the execution result that two loops are cross-executed (in order of execution), where the thread object is instantiated, the start () method is called, the ready state is entered, and the main function (for Loop) is preempted CPU, preemption to the first execution.

As you can see, the execution of threads is confusing and is not controllable.

Note: The thread object cannot call the run () method directly, and if it is called directly, it is single-threaded and there is no preemption concept.

Java Threading Understanding 001

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.