JAVA實現多線程入門

來源:互聯網
上載者:User

標籤:col   author   其他   ++   nbsp   print   cep   void   span   

1:程式員可以在程式中執行多個線程,每一個線程完成一個功能,並於其他線程並發執行,這種機制被稱為多線程

2:實現線程的兩種方法是,分別是繼承java.lang.Thread或者實現java.lang.Runnable介面

3:Thread類是java.lang包中的一個類,從這個類執行個體化的對象代表線程

4:完成線程真正功能的代碼是放在run()方法中的,當一個類繼承Thread類後,就可以在該類中覆蓋run()方法,將實現該線程功能的代碼寫入run()方法中,然後同時調用Thread 類中的strat()方法執行線程,也就是調用run()方法。

 1 package com.thread; 2 /** 3  * 1:程式員可以在程式中執行多個線程,每一個線程完成一個功能,並於其他線程並發執行,這種 4  * 機制被稱為多線程 5  * 2:實現線程的兩種方法是,分別是繼承java.lang.Thread或者實現java.lang.Runnable 6  * 介面 7  * 3:Thread淚時java.lang包中的一個類,從這個類執行個體化的對象代表線程 8  * 4:完成線程真正功能的代碼是放在run()方法中的,當一個淚繼承Thread類後,就可以 9  * 在該類中覆蓋run()方法,將實現該線程功能的代碼寫入run()方法中,然後同時調用Thread10  * 類中的strat()方法執行線程,也就是調用run()方法。11  * @author biexiansheng12  *13  */14 public class ThreadTest extends Thread {//指定類繼承Thread類15     16     private int count=10;17     public void run(){//重寫run()方法18         while(true){19             System.out.print(count+" ");//列印count變數20             if(--count==0){//使count變數自減,當自減為0時,退出迴圈21                 return;22             }23         }24     }25     public static void main(String[] args) {26         //new ThreadTest().start();27         Thread t=new ThreadTest();//兩種方法都可以實現線程運行28         t.start();29     }30 }

樣本如下

 1 package com.thread; 2 /** 3  * 1:多線程執行個體練習 4  * @author biexiansheng 5  * 6  */ 7 public class ThreadTest1 extends Thread{ 8  9     //多線程的實現都是在run()方法中實現的10     public void run(){11         System.out.println(getName()+"登上舞台");12         int count=0;//定義一個變數13         while(count<=100){14             System.out.println(getName()+"登上"+count+"次舞台");15             count++;16             if(count%10==0){17                 try {18                     Thread.sleep(2000);//捕獲異常,休眠2秒19                 } catch (InterruptedException e) {20                     // TODO Auto-generated catch block21                     e.printStackTrace();22                 }//23             }24         }25         System.out.println(getName()+"走下舞台");26     }27     public static void main(String[] args) {28         // TODO Auto-generated method stub29         Thread t=new ThreadTest1();30         t.setName("Mr.Thread");31         t.start();32         //new ThreadTest1().start();33         34         //實現Runnable介面35         Thread t1=new Thread(new RunnableTest(),"Ms.Runnable");36         t1.start();37     }38 }39 40 class RunnableTest implements Runnable{41 //寫完上一句,類名報錯,說明實現一個介面,就要實現這個介面內的方法42     @Override43     public void run() {44         // TODO Auto-generated method stub45         System.out.println(Thread.currentThread().getName()+"走上了舞台");46         //Runnable介面實現getName必須通過線程的currentThread()方法得到47         int count=0;//定義一個變數48         while(count<=100){49             System.out.println(Thread.currentThread().getName()+"登上"+count+"次舞台");50             count++;51             if(count%10==0){52                 try {53                     Thread.sleep(2000);//捕獲異常,休眠2秒54                 } catch (InterruptedException e) {55                     // TODO Auto-generated catch block56                     e.printStackTrace();57                 }//58             }59         }60         System.out.println(Thread.currentThread().getName()+"走下舞台");61     62     }63     64 }

 

JAVA實現多線程入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.