java多线程Runnable入门实例

复习一下多线程

xiancheng1.java

package multithreading;

public class xiancheng1 implements Runnable{

    @Override
    public void run() {
        
        // TODO Auto-generated method stub
        for (int i = 0; i < 10; i++) {
            System.out.println("线程1:  "+i);
        }
    }
  
}

xiancheng2.java

package multithreading;

public class xiancheng2 implements Runnable{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (int i = 0; i < 10; i++) {
            System.out.println("线程2:  "+i);
        }
    }

}

yun.java    主函数

package multithreading;

public class yun {
  public static void main(String[] args) {
    new Thread(new xiancheng1()).start();
    new Thread(new xiancheng2()).start();
    for (int i = 0; i < 10; i++) {
        System.out.println("线程3:    " +i);
    }
    
}
}

运行结果

线程1:  0
线程3:    0
线程1:  1
线程3:    1
线程1:  2
线程1:  3
线程3:    2
线程3:    3
线程3:    4
线程1:  4
线程3:    5
线程1:  5
线程2:  0
线程1:  6
线程1:  7
线程1:  8
线程3:    6
线程1:  9
线程3:    7
线程3:    8
线程3:    9
线程2:  1
线程2:  2
线程2:  3
线程2:  4
线程2:  5
线程2:  6
线程2:  7
线程2:  8
线程2:  9

你可能感兴趣的:(java)