Threads in the Java Foundation

Source: Internet
Author: User

How to create a thread:

 1. Inheriting the thread class

(1) Define class, Inherit thread

(2) The Void Run () method in the replication thread class (because the thread class is used to describe threads, the class defines a feature that stores the code that the thread wants to run.) This storage function is the Run method. )

(3) Call the thread's Start method, which can start the thread and call the Run method

Note: object. Run () is simply an object invocation method. And the thread was created, and not running;

Object. Start () to open the thread and execute the thread's Run method

  2. Implement the Runnable interface

(1) Define class, implement Runnable interface

(2) Overwrite the Run method in the Runnable interface. Store the code that the thread will run in the Run method

(3) Thread object creation through the thread class

(4) Pass the subclass object of the Runnable interface as the actual parameter to the constructor of the thread class. Because the object that the custom run method belongs to is the subclass object of the Runnable interface, to have the thread execute the Run method of the specified object, you must specify the object to which the Run method belongs.

(5) Call the Start method of the thread class to open the thread and call the Run method of the Runnable interface subclass

  The difference between the way of implementation and the way of inheritance:

   Implementation benefits: Avoids the limitations of single inheritance. It is recommended to use implementations when defining threads.

Inherit thread: The threading code is stored in the threads subclass Run method; runnable: The thread code holds the run method of the subclass of the interface.

Example of inheriting the thread class

 Public classTestThread2 { Public Static voidMain (string[] args) {MyThread2 Mt=NewMyThread2 ();                Mt.start ();  for(inti =0; I < -; i++) {System. out. println ("Testthread-----------"+i); }    }}classMyThread2 extends thread{ Public voidrun () { for(inti =0; I < -; i++) {System. out. println ("mythread========="+i); }    }}

Examples of implementing runnable interfaces:

 Public classTestthread { Public Static voidMain (string[] args) {MyThread Mt=NewMyThread (); Thread T=NewThread (MT);        T.start (); //Mt.run ();         for(inti =0; I < -; i++) {System. out. println ("Testthread-----------"+i); }    }}classMyThread implements Runnable { Public voidrun () { for(inti =0; I < -; i++) {System. out. println ("mythread========="+i); }    }    }

3. Threading Method:

Join method : Thread object. Join (), when a thread executes to the. Join () method of the B thread, a waits. When the b thread is executed, a will not execute. Join can be used to temporarily join thread execution.

 yield method : Pauses the currently executing thread object and executes other threads.

static Thread CurrentThread (): Gets the current thread object

 getName (): Gets the thread name

Set thread Name: SetName () or constructor

Threads in the Java Foundation

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.