Java Basics Multi-Threading (1)

Source: Internet
Author: User

1. Understanding thread A) for threads, it is easy to confuse the process, we use an example to distinguish them, each time a Word document is opened, this is a process, a word spelling check this is a thread, and then every landing a QQ, this is a process, QQ each session is a thread. b) for a computer with only one CPU, a point in time only one process occupies CPU2, Java implementation thread Java in implementing multithreading can be adopted: inherit the thread class, or implement Runnable interface a) inherit the thread class, This mode must overwrite the run () method
Class class name  extends thread{//member variable//member method public void Run () {//Thread body}}
Small example:
Import java.util.collection;import java.util.hashmap;import java.util.list;import java.util.map;class myThread Extends Thread {map<string,? extends collection<integer>> map= new hashmap<string,list<integer> > ();p rivate string name;public myThread (string name) {this.name  = name;} public void Run () {for (int i = 0; i < i++) {System.out.println (name + "Run, i=" +i);}}} public class MyThread {public static void main (string[] args) {MyThread mt1 = new MyThread ("Thread A"); MyThread mt2 = new Mythre AD ("Thread B"); Mt1.start (); <span style= "White-space:pre" ></span>mt2.start ();}}

b) Implement the Runnable interface and overwrite the run () method
public class Thread2 implements Runnable {///member variable//member method public void Run () {//Thread body}}
Small example:
Class A implements RUNNABLE{//member variable//member method private String name;public A (String name) {this.name = name;} public void Run () {//thread body for (int i = 0; i < i++) {System.out.println (name + "Run, i=" +i);}}} public class Thread2{public static void Maim (string[] args) {A A1 = new A ("Thread A"); A a2 = new A ("Thread B"); thread T1 = new Thread (A1); Thread t2 = new Thread (A2); T1.start (); T2.start ();}}
3, the difference between the two ways and the use of the conclusion a) using the Runnable interface can realize the sharing of resources, and the thread class does not. b) Avoid the limitations of single inheritance the use of runnable interfaces in development is more appropriate 4, the state of the thread
    • Create state: Prepare a Multithreaded object, thread t = new thread ();
    • Ready state: Call the Start () method, waiting for the CPU to dispatch
    • Run state: Execute Run () method
    • Blocking state: Temporarily stops execution and may use resources for other threads
    • Termination Status: Thread execution complete, no longer used

Java Basics Multi-Threading (1)

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.