How Java threads are created

Source: Internet
Author: User

① inherits the thread class (the Real threading Class) and is the implementation of the Runnable interface.

② implements the Runnable interface and overrides the Run method inside.

③ creates a thread pool using the executor framework. The executor framework is the implementation of the thread pool provided in the Juc.

Call the thread's start (): Start this thread; Call the appropriate run () method

A thread class that inherits from the thread class, you can call the Start method to start the thread directly (using static also enables resource sharing). A thread (object) can only execute a start () and cannot start a thread with the thread implementation of the Class object's run ().

Classes that implement the Runnable interface need to be wrapped again in the thread class before the Start method can be called. (Three thread objects wrap a class object, which enables resource sharing).

Use of threads, note the use of locks and synchronizations. (Multithreaded access to shared resources is prone to thread-safety issues)




In general, the second type is common.

* The Runnable interface has the following benefits:

*① avoids the limitations of point inheritance, a class can inherit multiple interfaces.

*② is suitable for sharing resources



/*

* Common methods of Thread:

* 1.start (): Starts the thread and executes the appropriate run () method

* 2.run (): The code to be executed by the child thread is put into the run () method

* 3.currentThread (): Static, tune the current thread

* 4.getName (): Gets the name of this thread

* 5.setName (): Sets the name of this thread

* 6.yield (): The thread calling this method frees the current CPU for execution (it is likely to grab the resource again)

* 7.join (): Invokes the Join () method of the B thread in a thread, indicating: When executed to this method, the a thread stops executing until the B thread finishes executing,

* A thread followed by join () followed by code execution

* 8.isAlive (): Determine if the current thread is still alive

* 9.sleep (Long L): explicitly let the current thread sleep l milliseconds (only catch exceptions, because the parent class run method does not throw exceptions)

* 10. Thread communication (method in object Class): Wait () notify () Notifyall ()

*

* Set the priority of the thread (not absolute, just a relatively high probability)

* GetPriority (): Return thread priority value

* setpriority (int newpriority): Change thread Priority

*/


How Java threads are created

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.