Multi-threading--Thread basics

Source: Internet
Author: User

    • Overview

 Multithreading is a very important piece of Java programming, in the multi-core CPU era, multithreading is the most direct way to improve the efficiency of program execution. So learning multithreading is very important to us.

    • Java Multi-Threading basic Application

  There are two ways of creating multithreading:

    1, inheriting the thread class

/** * @authoritunic.com * Inherit thread implementation Multithreading *@seeThread*/ Public classRunthreadextendsThread {/*** Thread execution content. */@Override Public voidrun () {System.out.println ("Thread Execution content!"); }     Public Static voidMain (string[] args) {//To create a thread objectRunthread RT =NewRunthread (); //Start RT ThreadRt.start (); }}

2, implement Runnable interface

/** * @authoritunic.com * Runnable interface for multithreading *@seeRunnable*/ Public classRunnablethreadImplementsRunnable {/*** Thread execution content. */@Override Public voidrun () {System.out.println ("Thread Execution content!"); }     Public Static voidMain (string[] args) {//To create a thread objectThread T =NewThread (NewRunnablethread ()); //start the T threadT.start (); }

    • 6 states of Java Multi-threading

  According to the official definition, the thread state is divided into six states:NEW,RUNNABLE,BLOCKED,waiting, timed_waiting, TERMINATED .

NEW: The thread has not started in that state. That is, after the new Thread, the state before the Start method is called.

  RUNNABLE: The thread is in the JVM's running state, but may be waiting for the operating system's execution schedule.

  BLOCKED: thread blocking state, which is usually caused by a critical resource pool, waiting for a "lock".

  waiting: usually refers to a result of a thread having an object lock and then entering the appropriate area after invoking the wait () method of the lock object. In addition, there are disguised ways of realization: Thread.Join (), Locksupport.park () and so on.

  timed_waiting: Similar to the waiting state, the difference is that the timed_waiting has a timeout setting, which automatically changes to the runnable state when it exceeds the set time. This state is implemented in the following ways: Thread.Sleep (long timeout), Thread.Join (long timeout), object.wait (long timeout), Locksupport.parknanos, Locksupport.parkuntil and so on.

  TERMINATED: The state is internal to the JVM as the new state and does not map to the thread state of the operating system. The state is the state after the thread has finished, indicating that the thread has finished executing.

    • Conclusion

This paper introduces the implementation and running state of Java Multi-threading, which is an introduction to the "in-depth multi-threading" series. This blog just want to make some of their own things to do some precipitation and reflection, convenient for me and people who need to consult. Interested in this series of friends can pay attention to me.

Multi-threading--Thread basics

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.