Java Thread--Inheriting Java.lang.Thread class implementation thread

Source: Internet
Author: User
Tags thread class

Many things in real life are done at the same time, in order to simulate this state in Java, the threading mechanism is introduced. First look at the basic concepts of threading.

A thread is an execution scenario in a process, which is the difference between executing a process, a process, and a thread:

1. Each process is an application and has a separate memory space.

2. Threads in the same process share memory and resources in their processes.

(Shared memory is heap memory and method area memory, stack memory is not shared, each thread has its own stack memory)

We also need to understand the following basic elements:

1. What is a process?

A process corresponds to an application. For example, starting Word on the Windows operating system means that a process is started. Starting the JVM in a Java development environment means that a process is started. Computers now support multiple processes, and in the same operating system, multiple processes can be started at the same time.

2. What is the role of multi-process?

A single-process computer can only do one thing.

Play the computer, while playing the game (the game process) while listening to music (music process).

For a single-core computer, is the game process and the music process running concurrently at the same point in time? No.

Because the CPU of the computer can only do one thing at a certain point in time. Since the computer will switch between "game progress" and "music process" frequently, the switching speed is very high, the human sense of the game and music at the same time.

The role of multi-process is not to improve execution speed, but to increase CPU utilization.

The memory between the process and the process is independent.

3. What is a thread?

A thread is the execution scenario in a process. A process can start multiple threads.

4. What is the role of multithreading?

Multithreading is not designed to improve execution speed, but to increase application usage.

Threads and Threads Share "heap memory and Method area memory", and stack memory is independent of one thread and one stack.

How does the 5.Java program work?

The Java command launches the Java Virtual machine, starts the JVM, and starts a process that automatically starts a "main thread" and then the main thread calls the main method of a class, so the main method runs in the main thread. All programs prior to this are single-threaded.

Theoretical knowledge, then try to analyze the following programs have a few threads?

PublicClassthreadtest01{
PublicStaticvoidMain string[] args) {
   m1 ();
 }
  public  static   void  M1 () {   
   m2 ();
 }
  public  static  void  m2 () {
   M3 ();
 }
  public  static   void  m3 () {
   system. out.println ( "Asfs");
 }
}

The above program has only one thread, that is, the main path. Main () method M1 (), M1 () m2 (), M2 () m3 (), so the main () method and M1 (); M2 (); M3 (); The method is in the same stack. Because the stack of processes and processes is independent, there is only one process.

Two ways to implement threading in Java are to inherit the Java.lang.Thread class and implement the Java.lang.Runnable interface, respectively. Let's look at the first implementation.

The first step: Inherit Java.lang.Thread;

Step two: Rewrite the Run method.

See the following code for specific procedures.

PublicClassthreadtest02{
PublicStaticvoidMainString[] (args) {
Create Thread (polymorphic: Parent type reference to sub-type object)
Thread t=NewProcessor ();
Start
T.start ();
for ( int j= 0;j< 10;j++) {
     system. out.println ( "Main--->" +j);
   }
 }
}
//Customizing a thread
class  processor&NBSP; extends thread{
  //Override Run Method
  public void&NBSP; run () {
    for ( Int i= 0;i< 100;i++) {
     system. OUT.PRINTLN ( Run---> +i);
   }
 }
}

Inheriting class Java.lang.Thread; the way to implement threading is divided into three steps, 1. customizing a thread; 2. Creating a thread; 3. Starting a thread;

Here's a diagram to see how the JVM works.

In the above code,

1. First customize thread Processor, create thread t=new Processor (), and belong to polymorphism, that is, the parent type refers to the subtype object.

2. Start the thread. T.start (); This thread executes an instantaneous end, telling the JVM to allocate a stack to the T thread. And the Run method does not require a manual call by the programmer, and the Run method is called automatically after the system thread starts.

3. The above program has two threads, one main thread, and one custom thread. With multithreading, the Main method ends just the main line stacks there is no method stack frame. But there are stack frames in the other stacks. The main method ends and the program may still be executing.

4. If you just call the Run method using the normal method, T.run (); Then there is only one thread, and the Run method will not execute as long as the For loop is not over.

Java Thread--Inheriting Java.lang.Thread class implementation thread

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.