本帖最后由 adamjy 于 2014-4-22 12:39 编辑
代码如下,如何只修改run()方法,让程序打印0 1 2 0 1 2或者0 0 1 1 2 2
- class MyRunnable implements Runnable{
- public void run(){
- for(int i= 0 ; i<3 ; i++){
- System.out.print(i);
- }
- }
- }
- public class Demo{
- public static void main(String args[]){
- MyRunnable runnable = new MyRunnable();
- //新建两个线程
- Thread t1 = new Thread (runnable);
- Thread t2 = new Thread (runnable);
- t1.start();
- t2.start();
- }
- }
复制代码 |