A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© adamjy 中级黑马   /  2014-4-21 23:13  /  1352 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 adamjy 于 2014-4-22 12:39 编辑

代码如下,如何只修改run()方法,让程序打印0 1 2 0 1 2或者0 0 1 1 2 2

  1. class MyRunnable implements Runnable{
  2.         public void run(){
  3.           for(int i= 0 ; i<3 ; i++){
  4.            System.out.print(i);
  5.           }
  6.         }
  7. }
  8. public class Demo{
  9.         public static void main(String args[]){
  10.           MyRunnable runnable = new MyRunnable();
  11.           //新建两个线程
  12.           Thread t1 = new  Thread (runnable);
  13.           Thread t2 = new  Thread (runnable);
  14.           t1.start();
  15.           t2.start();
  16.         }
  17. }
复制代码

7 个回复

倒序浏览
  1. public class ReflectTest3
  2. {
  3.          public static void main(String args[])throws Exception{
  4.               MyRunnable runnable = new MyRunnable();
  5.               //新建两个线程
  6.               Thread t1 = new  Thread (runnable);
  7.               Thread t2 = new  Thread (runnable);
  8.               t1.start();
  9.               t1.join();// 不知道这样行不
  10.               t2.start();
  11.              
  12.             }
  13. }
  14. class MyRunnable implements Runnable{
  15.     public void run(){
  16.       for(int i= 0 ; i<3 ; i++){
  17.        System.out.println(Thread.currentThread().getName()+" "+i);
  18.       }
  19.     }
  20. }
复制代码
回复 使用道具 举报
我运行的结果是
Thread-0 0
Thread-0 1
Thread-0 2
Thread-1 0
Thread-1 1
Thread-1 2
回复 使用道具 举报
在run()方法里加个锁就行了。代码如下:
  1. class MyRunnable implements Runnable{
  2.         Object obj = new Object();
  3.         public void run(){
  4.            synchronized(obj){
  5.                for(int i= 0 ; i<3 ; i++){
  6.                     System.out.print(i);
  7.                }
  8.            }
  9.         }
  10. }
  11. public class Demo{
  12.         public static void main(String args[]){
  13.           MyRunnable runnable = new MyRunnable();
  14.           //新建两个线程
  15.           Thread t1 = new  Thread (runnable);
  16.           Thread t2 = new  Thread (runnable);
  17.           t1.start();
  18.           t2.start();
  19.         }
  20. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 曹冬明 于 2014-4-21 23:39 编辑

加个锁
回复 使用道具 举报
加上同步锁就可以了
class MyRunnable implements Runnable{
    public void run(){
            synchronized (this) {
                       
               
      for(int i= 0 ; i<3 ; i++){
       System.out.print(i);
      }
            }
    }
}
回复 使用道具 举报
class MyRunnable implements Runnable{
    public void run(){
      for(int i= 0 ; i<3 ; i++){
       System.out.print(i);
       Thread.yield();         //这里添加个方法,让当前线程暂停执行一次
      }
    }
}
public class Demo1{
    public static void main(String args[]){
      MyRunnable runnable = new MyRunnable();
      //新建两个线程
      Thread t1 = new  Thread (runnable);
      Thread t2 = new  Thread (runnable);
      t1.start();
      t2.start();
    }
}
回复 使用道具 举报
  1. class MyRunnable implements Runnable{
  2.         Object obj = new Object();
  3.         public void run(){
  4.            synchronized(obj){
  5.                for(int i= 0 ; i<3 ; i++){
  6.                     System.out.print(i);
  7.                }
  8.            }
  9.         }
  10. }
  11. public class Demo{
  12.         public static void main(String args[]){
  13.           MyRunnable runnable = new MyRunnable();
  14.           Thread t1 = new  Thread (runnable);
  15.           Thread t2 = new  Thread (runnable);
  16.           t1.start();
  17.           t2.start();
  18.         }
  19. }
复制代码
只需要加个锁就成了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马