"Java Multithreading" What is the life cycle of threads and threads and how threads are created __java

Source: Internet
Author: User
Tags thread class

Q: What are processes and threads.

A: The process is a collection of all threads, each of which is a path of execution in the process, and main is also a thread, and the primary thread.

PS: Threads do not interfere with each other.

Q: Why use multithreading.

A: Improve the efficiency of the application, such as downloading software to download multiple tasks can be done at the same time is the application of multithreading. 1. The life cycle of threads
There are five states in the thread life cycle a) Create ( new thread) The thread is created when a new thread object is created with the operator.
The thread that is in the created state is just an empty thread object and the system does not allocate resources to it.

b The Ready (runnable) thread is ready to wait for the CPU to allocate resources for it. Start () is ready.
c) The running (running) thread obtains the CPU resources and is executing the code sequentially.
d) blocking (blocked) causes the thread to suspend execution because of sleep () or wait () or other thread join () and so on. The sleep () time ends or wakes up nontify () or another thread completes, and the thread enters the ready state from the blocking state.
e) The Dead (dead) thread executes, or is killed, that goes into extinction.
2. Three ways to create threads a) inherit the thread class Threaddemo1.java

The public class ThreadDemo1 extends thread{
	//Inheriting thread classes of the thread class should override the Run method to write the method that requires the thread to run in the Run method.
	@Override public
	void Run () {for
		(int i = 0; i < i++) {
			System.out.println ("Run i =" + i);
		}
	public
	static void Main (string[] args) {
		//directly creates an instance object that inherits the thread class
		Threaddemo Threaddemo = new Threaddemo ();
		The start thread should call Start () instead of call run (), or it will be executed sequentially rather than concurrently.
		Threaddemo.start ();
		for (int i = 0; i < i++) {
			System.out.println ("main i =" + i);}}


Result Fragment
Main i = 0
Run i = 0
main i = 1
run i = 1
main i = 2
run i = 2
main i = 3
run i = 3
  main i = 4
Run i = 4
Run i = 5
Run i = 6
run i = 7
Run i = 8
Run i = 9
run i = 10
  main i = 5
run i = one
main i = 6
run i = 12
You can see that the two threads are not completely alternating, and sometimes a thread executes more than one in effect, the CPU randomly allocates resources to provide execution opportunities.
b) Implement runnable interfaceThreaddemo2.java
public class ThreadDemo2 {public
	static void Main (string[] args) {
		//Create an instance object for the thread class that implements Runnable
		Newthread Newthread = new Newthread ();
		Creates an instance object for the thread class, passing in the Newthread.
		thread t = new thread (newthread);
		Invoke the Start () method to begin the thread
		T.start ();
		for (int i = 0; i < i++) {
			System.out.println ("main i =" + i);

}} Class Newthread implements runnable{
	//Implementation Runnable thread classes must override the run () method.
	@Override public
	void Run () {for
		(int i = 0; i < i++) {
			System.out.println ("Run i =" + i); 
  }}}
PS: Because Java single inheritance features multiple implementations, it is recommended that you create a thread using a method that implements the Runnable interface.
c) Using anonymous classesThreaddemo3.java
public class ThreadDemo3 {public
	static void Main (string[] args) {
		//pass in a Runnable object that overrides the run () method, calling start () directly.
		New Thread (new Runnable () {
			@Override public
			void Run () {for
				(int i = 0; i < i++) {
					System.out . println ("Run i =" + i);}}
		). Start ();
		
		for (int i = 0; i < i++) {
			System.out.println ("main i =" + i);}}

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.