java多线程的等待唤醒机制及如何解决同步过程中的安全问题

本文通过一个Java多线程程序实例,展示了如何解决多线程环境下资源共享导致的数据不一致问题。采用同步机制保证线程安全,具体实现包括使用synchronized关键字及wait和notify方法。
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
class Person{
   String name;
   String sex;
   boolean flag = true;
   public void setPerson(String name, String sex){ 
                 this.sex=sex;
                 this.name=name;  
   }
}
class Input implements Runnable{
   int x=0;
   Person p;
   Input(Person p){
      this.p=p;
   }
   public void run(){
       while(true){
           if(x==1){
              p.setPerson("hjz", "man");
           }
           else p.setPerson("哈哈哈", "女女女女");
           x=(x+1)%2;
       }
   }
}
 
class Output implements Runnable{
   int x=0;
   Person p;
   Output(Person p){
      this.p=p;
   }
   public void run(){
       while(true){
           System.out.println(p.name + "....." + p.sex);
       }
   }
}
public class Test{
    public static void main(String[] args){
         Person p = new Person();
         new Thread(new Input(p)).start();
         new Thread(new Output(p)).start();
    }
}
*/
 
/*
输出的结果:
哈哈哈.....man
hjz.....man
hjz.....man
哈哈哈.....man
hjz.....女女女女
*/
 
//线程安全隐患出现:首先考虑到是多线程操作了同一资源,所以要用同步!
/*
class Person{
   String name;
   String sex;
   boolean flag = true;
   public void setPerson(String name, String sex){ 
                 this.sex=sex;
                 this.name=name;  
   }
}
 
class Input implements Runnable{
   int x=0;
   Person p;
   Input(Person p){
      this.p=p;
   }
   public void run(){
       while(true){
          synchronized(new Object()){
               if(x==1){
                  p.setPerson("hjz", "man");
               }
               else p.setPerson("哈哈哈", "女女女女");
               x=(x+1)%2;
          }
       }
   }
}
 
class Output implements Runnable{
   int x=0;
   Person p;
   Output(Person p){
      this.p=p;
   }
   public void run(){
       while(true){
           System.out.println(p.name + "....." + p.sex);
       }
   }
}
public class Test{
    public static void main(String[] args){
         Person p = new Person();
         new Thread(new Input(p)).start();
         new Thread(new Output(p)).start();
    }
}
 */
 
//同步完成之后,发现还是出现安全隐患的情况,在考虑一下是否访问统一资源的多个线程用的是同一个锁!
//本例中的应将输入输出一起同步(注意输入输出不在同一个线程之中,输出线程不会获得 Person p对象的控制权!)
/*   class Input implements Runnable{
   int x=0;
   Person p;
    
   Input(Person p){
      this.p=p;
   }
   public void run(){
       while(true){
          synchronized(p){
               if(p.flag){
                 try{
                     p.wait();
                   }catch(InterruptedException e){
                   }
               }
               if(!p.flag){
                   if(x==1){
                      p.setPerson("hjz", "man");
                   }
                   else p.setPerson("哈哈哈", "女女女女");
                   x=(x+1)%2;
               }
                
               p.flag=true;
               p.notify();
                
          }
       }
   }
} */
 
 
//现在的代码是将同步放到函数里!真正开发过的时候就是这样实现,也就是我们多个线程同事操作一个类对象
//调用该类提供的对外方法,并将调用的方法进行同步!防止安全隐患!
class Person{
   String name;
   String sex;
   boolean flag = true;
   public void setPerson(String name, String sex){
       synchronized(this){
         if(!flag){
             try{
                wait();
             }catch(InterruptedException e){}
         }
         if(flag){
                 this.sex=sex;
                 try{
                    Thread.sleep(100);
                 }catch(InterruptedException e){}
                 this.name=name;
         }
         flag=false;
         notify();
       }
   }
    
   public void outPerson(){
      synchronized(this){
          if(flag){
             try{
                 wait();
             }catch(InterruptedException e){}
          }
          if(!flag){
              System.out.println(name + "....." + sex);
          }
          flag=true;
          notify();
      }
   }
}
 
class Input implements Runnable{
   int x=0;
   Person p;
    
   Input(Person p){
      this.p=p;
   }
   public void run(){
       while(true){
                   if(x==1){
                      p.setPerson("hjz""man");
                   }
                   else p.setPerson("哈哈哈""女女女女");
                   x=(x+1)%2;
       }
   }
}
 
class Output implements Runnable{
   int x=0;
   Person p;
   Output(Person p){
      this.p=p;
   }
   public void run(){
       while(true){
           p.outPerson();
       }
   }
 
 
public class Test{
    public static void main(String[] args){
         Person p = new Person();
         new Thread(new Input(p)).start();
         new Thread(new Output(p)).start();
    }
}









本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/3889183.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值