首页 > 代码库 > Runnable

Runnable

 

 

 1 package MyRunnable;
 2 
 3 public class MyRunnable implements Runnable{
 4     private String name ;
 5     public MyRunnable(String name){
 6         this.name=name;
 7     }
 8 @Override
 9     public void run() {
10     for(int i=0;i<5;i++){
11     System.out.println(name+i);
12     }
13         
14     }
15     public static void main(String[] args) {
16         
17 
18     }
19 
20     
21 
22 }

 

 

 1 package MyRunnable;
 2 
 3 public class RunnableTest {
 4 
 5     public static void main(String[] args) {
 6     MyRunnable r1 = new MyRunnable("线程A");
 7     MyRunnable r2 = new MyRunnable("线程B");
 8     new Thread(r1).start();
 9     new Thread(r2).start();
10     for(int i=0;i<5;i++){
11     System.out.println("主线程"+i);
12     }
13     }
14 
15 }

 

Runnable