java--Threads

Source: Internet
Author: User

I believe that in the study of Java people are expected to have a deep impression of the thread, and now when I started to touch Android development, really is actually discovering the thread is how important, and now my understanding of the thread to share to everyone.

We must distinguish between threads and processes is not the same thing, what is the process? The process is as if we need to execute the class file, and the thread is actually invoking the CPU resources to run. A class file typically has only one process, but threads can have many, and a thread executes asynchronously when executed.

No more nonsense, straight to the subject.

First, how to open a thread in the main function:

public class Thread_one {public    static void Main (String [] args) {        Run Run = new run ();        Run.run ();//This is a call to a method, and a thread has a great difference between threads thread        = new Thread (run);        Thread.Start ();//start thread, call the thread's run () method for        (int i=1; i<=20; i++) {            System.out.println ("Value of main thread I:--------" +i);        }    }} Class Run implements runnable{    @Override public    void Run () {for        (int i=1; i<=20; i++) {            System.out.println ("The value of the child thread I:" +i);}}}    

Second, the sleep method in the thread

public class Thread_sleep {     /* * Thread paused *    /public static void Main (String [] args) {        Runone run = new Run One ();        Thread thread = new thread (run);         Thread.Start ();        try {            thread.sleep ();            Thread.Interrupt ();//interrupt Thread Execution            //thread.stop ();//Relative break thread, stop is too rough, not recommended, generally use this method when you need to force a child thread to close        ( Interruptedexception e) {            e.printstacktrace ();}}    } Class Runone implements runnable{    @Override public    void Run () {for        (int i=1; i<10; i++) {            try {
   thread.sleep (+);                SYSTEM.OUT.PRINTLN ("-----" +new Date () + "-----");            } catch (Interruptedexception e) {                return;//close child thread directly when capture to child thread is interrupted}}                    }}    

Here is a special note, Thread.Interrupt (); Can be in the execution of thread break, relative to stop gentle so a little bit, but not the best way to close the thread, the following provides a way for everyone:

public class Thread_stop {public    static void Main (String [] args) {        runthree run = new Runthree ();        Thread th = new thread (run);        Th.start ();        try {            thread.sleep;        } catch (Interruptedexception e) {            e.printstacktrace ();        }        Run.setstop ();    }} Class Runthree implements runnable{    Boolean flag;    @Override public    Void Run () {        flag = true;        int i = 0;        while (flag) {            try {                System.out.println ("Child thread----" + (i++));                Thread.Sleep (+);            } catch (Interruptedexception e) {                e.printstacktrace ();}}    }    public void Setstop () {        flag = false;    }    }

Here's a brief introduction to merging and letting out in threads:

First, how to merge threads, call the Join () method here

public class Thread_join {     /* * Merge Thread *    /public static void Main (String [] args) {        runtwo run = new Runt Wo ();        Thread thread = new thread (run);        Thread.Start ();        try {            thread.join ();//merge thread, at this time equivalent to method call        } catch (Interruptedexception e) {            e.printstacktrace ();        }        for (int i=0; i<10; i++) {            System.out.println ("Main thread:" +i);}}}    Class Runtwo implements runnable{    @Override public    void Run () {for        (int i=0; i<10; i++) {            SYSTEM.OUT.PRINTLN ("Child thread:----" +i);}}}    

Second, how to let the thread, here called the yield () method of threads:

public class Thread_yield {    /** yields CPU     * @param args     *    /public static void main (string[] args) {        th th = New Th ("AAA");        Th.start ();        for (int i = 0; i<=10; i++) {            System.out.println ("main thread----" +i);}}}    Class th extends thread{    th () {}    th (String s) {super (s);}    @Override public    Void Run () {for        (int i = 0; i<=10; i++) {            if (i%3!=0) {                System.out.println ("Child thread" + i);            } else{                System.out.println ("Child thread i=" +i+ "thread to switch");                Yield ();//inherit from thread to use this method}}}}    

Finally, let's share with you some questions about thread priority:

public class Thread_priority {    /     * * Priority set thread priorities * Thread The     default priority is 5;thread with a maximum priority of 10 and a minimum of 0     *    / public static void Main (String [] args) {        T1 t1 = new T1 ();        T2 t2 = new    T2 ();        T1.start ();            T1.setpriority (thread.norm_priority+3);//Set T1 priority        T2.start ();}    } Class T1 extends thread{    @Override public    void Run () {for        (int i = 0; i<50; i++) {            System.out.print ln ("Thread T1-----" +i);}}}    Class T2 extends thread{    @Override public    void Run () {for        (int i = 0; i<50; i++) {            System.out.print ln ("Thread T2" +i);}}}    

I believe that people through the above code has basically understood the threading mechanism in Java, for thread synchronization, I will be in the next article and everyone to communicate, if there is any doubt welcome harassment.

java--Threads

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.