这回让宝宝在第一个线程里正数从1到100,在第二个线程里倒数也从1到100,在不同步的时候,可以看到,宝宝数乱了。。。
- package intro.thread;
- class Child2{
- public void positiveCount(){
- StringBuffer sb = new StringBuffer();
- for (int i = 1; i <= 100; i++) {
- // System.out.println(i);
- sb.append(i);
- sb.append(" ");
- if (i % 10 == 0) {
- System.out.println(sb.toString());
- sb.delete(0, sb.toString().length());
- }
- }
- }
-
- public void negativeCount(){
- StringBuffer sb = new StringBuffer();
- for (int i = 100; i >= 1; i--) {
- // System.out.println(i);
- sb.append(i);
- sb.append(" ");
- if (i % 10 == 0) {
- System.out.println(sb.toString());
- sb.delete(0, sb.toString().length());
- }
- }
- }
- }
- class ThreadA2 extends Thread{
- Child2 child = null;
- boolean positive = false;
- public ThreadA2(Child2 child, boolean positive){
- this.child = child;
- this.positive = positive;
- }
-
- public void run(){
- if(this.positive)
- child.positiveCount();
- else
- child.negativeCount();
- }
- }
- public class ThreadTest2 {
-
- public static void main(String [] args){
- Child2 child = new Child2();
- ThreadA2 A2_1 = new ThreadA2(child, true);
- ThreadA2 A2_2 = new ThreadA2(child, false);
- A2_1.start();
- A2_2.start();
- }
- }
结果:
- 100
- 99 98 97 96 95 94 93 92 91 90
- 89 88 87 86 85 84 83 82 81 80
- 1 2 3 4 5 6 7 8 9 10
- 11 12 13 14 15 16 17 18 19 20
- 21 22 23 24 25 26 27 28 29 30
- 31 32 33 34 35 36 37 38 39 40
- 41 42 43 44 45 46 47 48 49 50
- 51 52 53 54 55 56 57 58 59 60
- 79 78 77 76 75 74 73 72 71 70
- 69 68 67 66 65 64 63 62 61 60
- 61 62 63 64 65 66 67 68 69 70
- 71 72 73 74 75 76 77 78 79 80
- 59 58 57 56 55 54 53 52 51 50
- 81 82 83 84 85 86 87 88 89 90
- 49 48 47 46 45 44 43 42 41 40
- 91 92 93 94 95 96 97 98 99 100
- 39 38 37 36 35 34 33 32 31 30
- 29 28 27 26 25 24 23 22 21 20
- 19 18 17 16 15 14 13 12 11 10
那么我们对线程同步一下,让宝宝正数到100后再倒数回1,
- package intro.thread;
- class Child2 {
- public void positiveCount() {
- synchronized (this) {
- StringBuffer sb = new StringBuffer();
- for (int i = 1; i <= 100; i++) {
- // System.out.println(i);
- sb.append(i);
- sb.append(" ");
- if (i % 10 == 0) {
- System.out.println(sb.toString());
- sb.delete(0, sb.toString().length());
- }
- }
- }
- }
- public void negativeCount() {
- synchronized (this) {
- StringBuffer sb = new StringBuffer();
- for (int i = 100; i >= 1; i--) {
- // System.out.println(i);
- sb.append(i);
- sb.append(" ");
- if (i % 10 == 0) {
- System.out.println(sb.toString());
- sb.delete(0, sb.toString().length());
- }
- }
- }
- }
- }
- class ThreadA2 extends Thread {
- Child2 child = null;
- boolean positive = false;
- public ThreadA2(Child2 child, boolean positive) {
- this.child = child;
- this.positive = positive;
- }
- public void run() {
- if (this.positive)
- child.positiveCount();
- else
- child.negativeCount();
- }
- }
- public class ThreadTest2 {
- public static void main(String[] args) {
- Child2 child = new Child2();
- ThreadA2 A2_1 = new ThreadA2(child, true);
- ThreadA2 A2_2 = new ThreadA2(child, false);
- A2_1.start();
- A2_2.start();
- }
- }
结果:
- 1 2 3 4 5 6 7 8 9 10
- 11 12 13 14 15 16 17 18 19 20
- 21 22 23 24 25 26 27 28 29 30
- 31 32 33 34 35 36 37 38 39 40
- 41 42 43 44 45 46 47 48 49 50
- 51 52 53 54 55 56 57 58 59 60
- 61 62 63 64 65 66 67 68 69 70
- 71 72 73 74 75 76 77 78 79 80
- 81 82 83 84 85 86 87 88 89 90
- 91 92 93 94 95 96 97 98 99 100
- 100
- 99 98 97 96 95 94 93 92 91 90
- 89 88 87 86 85 84 83 82 81 80
- 79 78 77 76 75 74 73 72 71 70
- 69 68 67 66 65 64 63 62 61 60
- 59 58 57 56 55 54 53 52 51 50
- 49 48 47 46 45 44 43 42 41 40
- 39 38 37 36 35 34 33 32 31 30
- 29 28 27 26 25 24 23 22 21 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)不会起到同步作用,结果混乱
- public static void main(String[] args) {
- Child2 child1 = new Child2();
- Child2 child2 = new Child2();
- ThreadA2 A2_1 = new ThreadA2(child1, true);
- ThreadA2 A2_2 = new ThreadA2(child2, false);
- A2_1.start();
- A2_2.start();
- }
结果:
- 1 2 3 4 5 6 7 8 9 10
- 11 12 13 14 15 16 17 18 19 20
- 21 22 23 24 25 26 27 28 29 30
- 31 32 33 34 35 36 37 38 39 40
- 41 42 43 44 45 46 47 48 49 50
- 51 52 53 54 55 56 57 58 59 60
- 61 62 63 64 65 66 67 68 69 70
- 100
- 99 98 97 96 95 94 93 92 91 90
- 71 72 73 74 75 76 77 78 79 80
- 89 88 87 86 85 84 83 82 81 80
- 79 78 77 76 75 74 73 72 71 70
- 81 82 83 84 85 86 87 88 89 90
- 69 68 67 66 65 64 63 62 61 60
- 59 58 57 56 55 54 53 52 51 50
- 49 48 47 46 45 44 43 42 41 40
- 39 38 37 36 35 34 33 32 31 30
- 29 28 27 26 25 24 23 22 21 20
- 19 18 17 16 15 14 13 12 11 10
- 91 92 93 94 95 96 97 98 99 100
阅读(3298) | 评论(1) | 转发(0) |