Java: Multithreading

Source: Internet
Author: User

When the program runs, it is actually the CPU in the process of executing the program, in order to improve the efficiency of a process can have multiple threads.

Multithreading in Java:

In fact, we've seen Java threads before, and main is a thread in Java, and another thread is always running at the same time as main, which is the garbage collection thread.

Java threads have four states:

1, created: New Thread

2, run (Ready): The JVM calls the thread's Start method to execute the contents of the thread's run

3, Freeze: Sleep (ms), wait ()-notify ()

4, extinction

Java can implement multithreading techniques by inheriting the thread class and implementing the Runnable interface.

Multithreading is implemented by inheriting the thread class:

Method:

1. Create a custom class to inherit the thread class

2, overriding the Run method in the thread class

3. Create a child class object of a custom thread class, that is, the thread object is created

4, call the Start method to get the JVM to start a custom thread (in fact, tell the JVM to run the contents of the customized Run method)

classDefinedthreadextendsThread//custom Thread class inherits the thread class{    ...     Public voidrun () {...//Duplicate the Run method in the thread class,define what the thread is doing    }    ...}classMultthread { Public Static voidMain (string[] args)//Main is a thread{definedthread dt=NewDefinedthread ();//To create a thread objectDt.start ();//Start Thread    }}
Multithreading is achieved by implementing the Runnable interface:

1. Creating a custom class implementation runnable interface

2, overwrite the Run method in the interface

3. Create a thread object from the thread class and pass the object of the custom class as a parameter to the constructor of the thread class

The 4,thread class object calls the Start method to open the thread

classDefinedthreadImplementsRunnable//custom class implementation Runnable interface{    ...     Public voidrun () {...//The run method of the replication interface that defines what the thread is doing    }    ...}classMultthread { Public Static voidMain (string[] args)//Main is a thread{definedthread dt=NewDefinedthread (); Thread T=NewThread (DT);//passing a subclass object of a custom class as a parameter to the thread constructorT.start ();//Thread Object T calls the Start method to open a thread    }}
The relationship between thread and runnable:

1, a class can inherit only one class, but the implementation of multiple interfaces, in this case, thread has limitations, runnable extensibility is good

2,thread Object calls thread (Runnable run) or thread (Runnable run, String name) to construct a thread, using resources within the same Runnable instance object, Runnable is suitable for resource sharing. (Of course, inheriting the thread class, using static variables can also share resources, but this is cumbersome and static life cycle long)

3, through the definition of the thread class public class Thread extends Object implements Runnable, you can see that the thread class actually also implements the Runnable interface, but there is a start method in thread, And runnable does not, so the content of the run that implements the Runnable object wants the JVM to be called and must pass the object to thread, and the thread's object calls the Start method to start the threads and then runs the contents of the run.

Java: Multithreading

Related Article

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.