Java Learning Note (38)-Create a thread that implements the Runnable interface

Source: Internet
Author: User

To create a thread that implements the Runnable interface
/* * Create thread mode two: Implement Runnable interface * Step: * 1. Create an implementation class for the Runnable interface * 2. Implement the Run method * 3. Create an instance of the implementation class * 4. Create a thread instance and pass an instance of the implementation class as a parameter * 5. Call S Tart method, starts the thread and runs the Run method * / class Mydemo implements Runnable{@Override Public voidRun () { for(inti =1; I <= -; i++) {System.out.println (Thread.CurrentThread (). GetName () +"*****run*******"+ i); }    }} Public  class Test03 {     Public Static voidMain (string[] args) {//Create an instance of the Runnable interface implementation classMydemo d=NewMydemo ();//create thread to pass in an instance of the implementation classThread t=NewThread (D,"MyThread");//Start thread and Execute Run methodT.start (); Thread Thread=thread.currentthread ();//Get current thread instance         for(inti =1; I <= -; i++) {System.out.println (Thread.getname () +"*****hello world*******"+ i); }    }}
Multithreading
/* Multithreading * phenomenon: The output may not be output sequentially * cause: Although the output code is executed, the result may not be printed, but the other thread is printing ahead of time */ Public  class Test04 {     Public Static voidMain (string[] args) {/*mythread1 t1 = new MyThread1 ();        MyThread1 t2 = new MyThread1 ();        T1.start (); T2.start (); */MyThread2 mt=NewMyThread2 (); Thread t1=NewThread (MT,"Window 1"); Thread t2=NewThread (MT,"Window 2");        T1.start ();    T2.start (); }} class MyThread1 extends Thread {    //generally do not use static, cause: 1.static initialized at class load 2. Life cycle too long    Static intnum = -;//Number of votes@Override Public voidRun () { while(Num >0) {System.out.println (Thread.CurrentThread (). GetName () +"******"+ num--); }    }} class MyThread2 implements Runnable {    intnum = the; @Override Public voidRun () { while(Num >0) {System.out.println (Thread.CurrentThread (). GetName () +"******"+ num--); }    }}

Java Learning Note (38)-Create a thread that implements the Runnable interface

Related Article

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.