標籤:啟動 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