Chinaunix首页 | 论坛 | 博客
  • 博客访问: 741728
  • 博文数量: 130
  • 博客积分: 2951
  • 博客等级: 少校
  • 技术积分: 1875
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-04 18:32
文章分类

全部博文(130)

文章存档

2013年(1)

2012年(129)

分类: Java

2012-03-29 17:17:22

这回让宝宝在第一个线程里正数从1到100,在第二个线程里倒数也从1到100,在不同步的时候,可以看到,宝宝数乱了。。。


点击(此处)折叠或打开

  1. package intro.thread;

  2. class Child2{
  3.     public void positiveCount(){
  4.         StringBuffer sb = new StringBuffer();
  5.         for (int i = 1; i <= 100; i++) {
  6.             // System.out.println(i);
  7.             sb.append(i);
  8.             sb.append(" ");
  9.             if (i % 10 == 0) {
  10.                 System.out.println(sb.toString());
  11.                 sb.delete(0, sb.toString().length());
  12.             }
  13.         }
  14.     }
  15.     
  16.     public void negativeCount(){
  17.         StringBuffer sb = new StringBuffer();
  18.         for (int i = 100; i >= 1; i--) {
  19.             // System.out.println(i);
  20.             sb.append(i);
  21.             sb.append(" ");
  22.             if (i % 10 == 0) {
  23.                 System.out.println(sb.toString());
  24.                 sb.delete(0, sb.toString().length());
  25.             }
  26.         }
  27.     }
  28. }

  29. class ThreadA2 extends Thread{
  30.     Child2 child = null;
  31.     boolean positive = false;
  32.     public ThreadA2(Child2 child, boolean positive){
  33.         this.child = child;
  34.         this.positive = positive;
  35.     }
  36.     
  37.     public void run(){
  38.         if(this.positive)
  39.             child.positiveCount();
  40.         else
  41.             child.negativeCount();
  42.     }    
  43. }


  44. public class ThreadTest2 {
  45.     
  46.     public static void main(String [] args){
  47.         Child2 child = new Child2();
  48.         ThreadA2 A2_1 = new ThreadA2(child, true);
  49.         ThreadA2 A2_2 = new ThreadA2(child, false);
  50.         A2_1.start();
  51.         A2_2.start();
  52.     }

  53. }
结果:

点击(此处)折叠或打开

  1. 100
  2. 99 98 97 96 95 94 93 92 91 90
  3. 89 88 87 86 85 84 83 82 81 80
  4. 1 2 3 4 5 6 7 8 9 10
  5. 11 12 13 14 15 16 17 18 19 20
  6. 21 22 23 24 25 26 27 28 29 30
  7. 31 32 33 34 35 36 37 38 39 40
  8. 41 42 43 44 45 46 47 48 49 50
  9. 51 52 53 54 55 56 57 58 59 60
  10. 79 78 77 76 75 74 73 72 71 70
  11. 69 68 67 66 65 64 63 62 61 60
  12. 61 62 63 64 65 66 67 68 69 70
  13. 71 72 73 74 75 76 77 78 79 80
  14. 59 58 57 56 55 54 53 52 51 50
  15. 81 82 83 84 85 86 87 88 89 90
  16. 49 48 47 46 45 44 43 42 41 40
  17. 91 92 93 94 95 96 97 98 99 100
  18. 39 38 37 36 35 34 33 32 31 30
  19. 29 28 27 26 25 24 23 22 21 20
  20. 19 18 17 16 15 14 13 12 11 10
那么我们对线程同步一下,让宝宝正数到100后再倒数回1,

点击(此处)折叠或打开

  1. package intro.thread;

  2. class Child2 {
  3.     public void positiveCount() {
  4.         synchronized (this) {
  5.             StringBuffer sb = new StringBuffer();
  6.             for (int i = 1; i <= 100; i++) {
  7.                 // System.out.println(i);
  8.                 sb.append(i);
  9.                 sb.append(" ");
  10.                 if (i % 10 == 0) {
  11.                     System.out.println(sb.toString());
  12.                     sb.delete(0, sb.toString().length());
  13.                 }
  14.             }
  15.         }
  16.     }

  17.     public void negativeCount() {
  18.         synchronized (this) {
  19.             StringBuffer sb = new StringBuffer();
  20.             for (int i = 100; i >= 1; i--) {
  21.                 // System.out.println(i);
  22.                 sb.append(i);
  23.                 sb.append(" ");
  24.                 if (i % 10 == 0) {
  25.                     System.out.println(sb.toString());
  26.                     sb.delete(0, sb.toString().length());
  27.                 }
  28.             }
  29.         }
  30.     }
  31. }

  32. class ThreadA2 extends Thread {
  33.     Child2 child = null;
  34.     boolean positive = false;

  35.     public ThreadA2(Child2 child, boolean positive) {
  36.         this.child = child;
  37.         this.positive = positive;
  38.     }

  39.     public void run() {
  40.         if (this.positive)
  41.             child.positiveCount();
  42.         else
  43.             child.negativeCount();
  44.     }
  45. }

  46. public class ThreadTest2 {

  47.     public static void main(String[] args) {
  48.         Child2 child = new Child2();
  49.         ThreadA2 A2_1 = new ThreadA2(child, true);
  50.         ThreadA2 A2_2 = new ThreadA2(child, false);
  51.         A2_1.start();
  52.         A2_2.start();
  53.     }

  54. }
结果:

点击(此处)折叠或打开

  1. 1 2 3 4 5 6 7 8 9 10
  2. 11 12 13 14 15 16 17 18 19 20
  3. 21 22 23 24 25 26 27 28 29 30
  4. 31 32 33 34 35 36 37 38 39 40
  5. 41 42 43 44 45 46 47 48 49 50
  6. 51 52 53 54 55 56 57 58 59 60
  7. 61 62 63 64 65 66 67 68 69 70
  8. 71 72 73 74 75 76 77 78 79 80
  9. 81 82 83 84 85 86 87 88 89 90
  10. 91 92 93 94 95 96 97 98 99 100
  11. 100
  12. 99 98 97 96 95 94 93 92 91 90
  13. 89 88 87 86 85 84 83 82 81 80
  14. 79 78 77 76 75 74 73 72 71 70
  15. 69 68 67 66 65 64 63 62 61 60
  16. 59 58 57 56 55 54 53 52 51 50
  17. 49 48 47 46 45 44 43 42 41 40
  18. 39 38 37 36 35 34 33 32 31 30
  19. 29 28 27 26 25 24 23 22 21 20
  20. 19 18 17 16 15 14 13 12 11 10

上面的positiveCount和negativeCount方法都加了synchronize关键字,如果只给一个方法标注,最后的结果就会混乱,所以说:
1. 当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块 (导致结果混乱)
2. 当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。(结果有序)

同样,如果是两个不同的宝宝,一个正数,一个倒数,synchronized(this)不会起到同步作用,结果混乱

点击(此处)折叠或打开

  1. public static void main(String[] args) {
  2.         Child2 child1 = new Child2();
  3.         Child2 child2 = new Child2();
  4.         ThreadA2 A2_1 = new ThreadA2(child1, true);
  5.         ThreadA2 A2_2 = new ThreadA2(child2, false);
  6.         A2_1.start();
  7.         A2_2.start();
  8.     }
结果:

点击(此处)折叠或打开

  1. 1 2 3 4 5 6 7 8 9 10
  2. 11 12 13 14 15 16 17 18 19 20
  3. 21 22 23 24 25 26 27 28 29 30
  4. 31 32 33 34 35 36 37 38 39 40
  5. 41 42 43 44 45 46 47 48 49 50
  6. 51 52 53 54 55 56 57 58 59 60
  7. 61 62 63 64 65 66 67 68 69 70
  8. 100
  9. 99 98 97 96 95 94 93 92 91 90
  10. 71 72 73 74 75 76 77 78 79 80
  11. 89 88 87 86 85 84 83 82 81 80
  12. 79 78 77 76 75 74 73 72 71 70
  13. 81 82 83 84 85 86 87 88 89 90
  14. 69 68 67 66 65 64 63 62 61 60
  15. 59 58 57 56 55 54 53 52 51 50
  16. 49 48 47 46 45 44 43 42 41 40
  17. 39 38 37 36 35 34 33 32 31 30
  18. 29 28 27 26 25 24 23 22 21 20
  19. 19 18 17 16 15 14 13 12 11 10
  20. 91 92 93 94 95 96 97 98 99 100











阅读(3241) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

zhangzhaofeng2012-03-30 11:48:59

两个不同的宝宝 ThreadLocal