Multi-threaded Knowledge Base

Source: Internet
Author: User
Tags thread class knowledge base

Basic concepts

 process is the execution of a task, is a program and its data in the processing machine sequential execution of the activity occurs, the popular point of the Windows system each running EXE is a process, a process has multiple threads

  threads can be understood as subtasks that run independently in a process.

Why use Multithreading

It says that threads are sub-tasks that run independently in a process, single-threaded, that is, when a single task, each task is executed sequentially, synchronously. For example, there are two tasks A and b,a tasks are waiting to download the file, the time is 5s,cpu in the waiting state, the b task only needs 1s, even if the b task is just 1s, also need a 5s execution end to execute B.

Multi-threading, the CPU can switch between task A and task B, so that task B does not need to wait for a after execution, improve the efficiency of the system, which is the advantage of multithreading, that is, asynchronous.

How threads are Created

Way one: Inherit thread, the disadvantage is that Java only supports single inheritance, actually see the source code thread class is also implemented Runnable interface

Method Two (recommended): Implement the Runnable interface, generally use this method (when the class itself has inherited a class, you can use to implement the interface this way)

Inherit thread mode

public class MyThread2 extends thread{    @Override public    void Run () {        try {for            (int i=0;i<10;i++) {                int time = (int) (Math.random () * +);                Thread.Sleep (time);                System.out.println ("run=" + thread.currentthread (). GetName ());            }        } catch (Interruptedexception e) {            e.printstacktrace ();        }    }    public static void Main (string[] args) {        MyThread2 myThread2 = new MyThread2 ();        Mythread2.start ();//start thread        try {for            (int i=0;i<10;i++) {                int time = (int) (Math.random () *1000);                Thread.Sleep (time);                System.out.println ("main=" +thread.currentthread (). GetName ());            }        } catch (Interruptedexception e) {            e.printstacktrace ();}}    }

Run results

run=thread-0Main=mainrun=thread-0main=mainrun=thread-0Run= Thread-0Main=mainrun=thread-0main=mainrun=thread-0main=  Mainrun=thread-0main=mainmain=Mainmain=mainrun=thread-0  Run=thread-0main=mainmain=mainrun=thread-0

From the results of the run: Code run results and code execution order independent, the above code has an asynchronous effect

The Thread.Start () method is to start a thread, and if you execute run directly (), it is actually equivalent to executing a method in Java order.

Implementing the runnable Approach

public class Myrunnable implements Runnable {public    void run () {        System.out.println (Thread.CurrentThread (). GetName ());    }    public static void Main (string[] args) {        myrunnable runnable = new myrunnable ();        Thread thread = new Thread (runnable);        Thread.Start ();        System.out.println ("end!!");}    }

  

Run results

end!! Thread-0

  

There are only two types of thread implementations, but there are many forms of expression

Example 1:


public static void Main (string[] args) { thread thread = new Thread (new Runnable () {public void run () { syste M.out.println (Thread.CurrentThread (). GetName ()); } ); Thread.Start (); System.out.println ("end!!"); }

Example 2:

public static void Main (string[] args) {        Runnable Runnable = new Runnable () {public            void run () {                SYSTEM.OUT.P Rintln (Thread.CurrentThread (). GetName ());            }        ;        Thread thread = new Thread (runnable);        Thread.Start ();        System.out.println ("end!!");}

  

 

Multi-threaded Knowledge Base

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.