首页 > 代码库 > java多线线程 1

java多线线程 1

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     @Override
41     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