java多線線程 1

來源:互聯網
上載者:User

標籤:啟動   actor   end   sleep   als   ++   code   print   false   

java對線程的支援
java.lang
class Thread\interface Runnable run()方法
線程的建立和啟動
建立
Thread()
Thread(String name)
Thread(Runnable target)
Thread(Runnable target,String name)
常見方法
void start() 啟動線程

 1 package com.imooc.test; 2  3 public class Actor extends Thread { 4     public void run(){ 5         System.out.println(getName()+"是一個演員!"); 6         int count = 0; 7         boolean keepRunning = true; 8         while(keepRunning){ 9             System.out.println(getName()+"登台演出:" + (++count));10             11             if(count==100){12                 keepRunning = false;13             }14             15             if(count%10==0){16                 try {17                     Thread.sleep(1000);18                 } catch (InterruptedException e) {19                     e.printStackTrace();20                 }21             }22         }23         System.out.println(getName()+"的演出結束了!");24     }25     26     public static void main(String[] args) {27         Thread actor = new Actor();28         actor.setName("Mr.Thread");29         30         actor.start();31         32         Thread actressThread = new Thread(new Actress(),"Ms.Runnable");33         34         actressThread.start();35     }36 }    37 38 class Actress implements Runnable{39 40     @Override41     public void run() {42         System.out.println(Thread.currentThread().getName()+"是一個演員!");43         int count = 0;44         boolean keepRunning = true;45         while(keepRunning){46             System.out.println(Thread.currentThread().getName()+"登台演出:" + (++count));47             48             if(count==100){49                 keepRunning = false;50             }51             52             if(count%10==0){53                 try {54                     Thread.sleep(1000);55                 } catch (InterruptedException e) {56                     e.printStackTrace();57                 }58             }59         }60         System.out.println(Thread.currentThread().getName()+"的演出結束了!");        61     }62     63 }

 

java多線線程 1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.