Java multithreading Learning 1-inherit the Thread class and implement the runnable interface to create a thread

Source: Internet
Author: User
Package learn. thread;/*** use the Thread class to create a thread. The start () method (the run () method is automatically called) and the run () method, * To implement your own thread, rewrite the run () method. * @ author whitetiger **/public class learnone {public static void main (string [] ARGs) {demo = new demo (); demo. start (); // start the thread while (true) {system. out. println (thread. currentthread (). getname () + ">>>> main method") ;}} class demo extends thread {@ overridepublic void run () {While (true) {system. out. println (thread. currentthread (). getname () + "learnone ");}}}

Package learn. thread;/*** to implement the runnable interface, you must implement the run method unless it is an abstract class * declaring new A (); * Call New thread (). start (); * to implement your own thread, rewrite the run () method * @ author whitetiger **/public class learntwo {

Public static void main (string [] ARGs) {demo2 de = new demo2 (); New thread (de ). start (); While (true) {system. out. println (thread. currentthread (). getname () + ">>> main thread ");}}}

Class demo2 implements runnable {

@ Override public void run () {// todo auto-generated method stub while (true) {system. out. println (thread. currentthread (). getname () + "in-thread ");}}}


 

 

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.