Java教程

java多线程-用对象锁的方式控制线程同步

本文主要是介绍java多线程-用对象锁的方式控制线程同步,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

java多线程操作同一块内存数据的时候,就可能造成数据混乱,也就是常用的线程安全问题。初步的一个解决方案是通过synchronized关键字来解决问题。

先看下面的连个例子:

public class ThreadSychronizedTest {

 /**
  * 线程操作以及加上对象锁-ThreadSychronizedTest1对比
  */
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 //创建线程并启动线程1
      MyThread a=new MyThread();
      a.start();
    //启动线程2
      new MyThread().start();
    //启动线程3 
      new MyThread().start();
 }
}
class MyThread extends Thread
{
 public static int index=0;
@Override
//循环输出累加值
public void run() {
 for(int i=0;i<100;i++)
 {
  System.out.println(this.getName()+":"+index++);
 }
}
}

这个例子的输出结果:

Thread-0:0
Thread-1:1
Thread-1:3
Thread-1:4
Thread-1:5
Thread-1:6
Thread-0:2
Thread-0:7...........................

从这个结果可以看出,三个线程都去访问一个静态的变量index,由于它们获取系统的时间片的时刻是不确定的,因此它们对index的访问和修改是存在穿插。

例子2改进例子1

public class ThreadSychronizedTest1 {

 /**
  * 线程操作以及加上对象锁
  */
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 //创建线程并启动线程1
      MyThread1 a=new MyThread1();
      a.start();
    //启动线程2
      new MyThread1().start();
    //启动线程3 
      new MyThread1().start();
 }
}
class MyThread1 extends Thread
{
 public static int index=0;
 //添加一个锁对象实现同步
 public static Object obj=new Object();
@Override
//循环输出累加值
public void run() {
 synchronized(obj){
 for(int i=0;i<100;i++)
 {
  System.out.println(this.getName()+":"+index++);
 }
 }
}
}

输出结果:

Thread-0:0
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-0:5
Thread-0:6
Thread-0:7
Thread-0:8
Thread-0:9
Thread-0:10
Thread-0:11
Thread-0:12
Thread-0:13
Thread-0:14
Thread-0:15
Thread-0:16
Thread-0:17
Thread-0:18
Thread-0:19
Thread-0:20
Thread-0:21
Thread-0:22
Thread-0:23
Thread-0:24
Thread-0:25
Thread-0:26
Thread-0:27
Thread-0:28
Thread-0:29
Thread-0:30
Thread-0:31
Thread-0:32
Thread-0:33
Thread-0:34
Thread-0:35
Thread-0:36
Thread-0:37
Thread-0:38
Thread-0:39
Thread-0:40
Thread-0:41
Thread-0:42
Thread-0:43
Thread-0:44
Thread-0:45
Thread-0:46
Thread-0:47
Thread-0:48
Thread-0:49
Thread-0:50
Thread-0:51
Thread-0:52
Thread-0:53
Thread-0:54
Thread-0:55
Thread-0:56
Thread-0:57
Thread-0:58
Thread-0:59
Thread-0:60
Thread-0:61
Thread-0:62
Thread-0:63
Thread-0:64
Thread-0:65
Thread-0:66
Thread-0:67
Thread-0:68
Thread-0:69
Thread-0:70
Thread-0:71
Thread-0:72
Thread-0:73
Thread-0:74
Thread-0:75
Thread-0:76
Thread-0:77
Thread-0:78
Thread-0:79
Thread-0:80
Thread-0:81
Thread-0:82
Thread-0:83
Thread-0:84
Thread-0:85
Thread-0:86
Thread-0:87
Thread-0:88
Thread-0:89
Thread-0:90
Thread-0:91
Thread-0:92
Thread-0:93
Thread-0:94
Thread-0:95
Thread-0:96
Thread-0:97
Thread-0:98
Thread-0:99
Thread-2:100
Thread-2:101
Thread-2:102
Thread-2:103
Thread-2:104
Thread-2:105
Thread-2:106
Thread-2:107
Thread-2:108
Thread-2:109
Thread-2:110
Thread-2:111
Thread-2:112
Thread-2:113
Thread-2:114
Thread-2:115
Thread-2:116
Thread-2:117
Thread-2:118
Thread-2:119
Thread-2:120
Thread-2:121
Thread-2:122
Thread-2:123
Thread-2:124
Thread-2:125
Thread-2:126
Thread-2:127
Thread-2:128
Thread-2:129
Thread-2:130
Thread-2:131
Thread-2:132
Thread-2:133
Thread-2:134
Thread-2:135
Thread-2:136
Thread-2:137
Thread-2:138
Thread-2:139
Thread-2:140
Thread-2:141
Thread-2:142
Thread-2:143
Thread-2:144
Thread-2:145
Thread-2:146
Thread-2:147
Thread-2:148
Thread-2:149
Thread-2:150
Thread-2:151
Thread-2:152
Thread-2:153
Thread-2:154
Thread-2:155
Thread-2:156
Thread-2:157
Thread-2:158
Thread-2:159
Thread-2:160
Thread-2:161
Thread-2:162
Thread-2:163
Thread-2:164
Thread-2:165
Thread-2:166
Thread-2:167
Thread-2:168
Thread-2:169
Thread-2:170
Thread-2:171
Thread-2:172
Thread-2:173
Thread-2:174
Thread-2:175
Thread-2:176
Thread-2:177
Thread-2:178
Thread-2:179
Thread-2:180
Thread-2:181
Thread-2:182
Thread-2:183
Thread-2:184
Thread-2:185
Thread-2:186
Thread-2:187
Thread-2:188
Thread-2:189
Thread-2:190
Thread-2:191
Thread-2:192
Thread-2:193
Thread-2:194
Thread-2:195
Thread-2:196
Thread-2:197
Thread-2:198
Thread-2:199
Thread-1:200
Thread-1:201
Thread-1:202
Thread-1:203
Thread-1:204
Thread-1:205
Thread-1:206
Thread-1:207
Thread-1:208
Thread-1:209
Thread-1:210
Thread-1:211
Thread-1:212
Thread-1:213
Thread-1:214
Thread-1:215
Thread-1:216
Thread-1:217
Thread-1:218
Thread-1:219
Thread-1:220
Thread-1:221
Thread-1:222
Thread-1:223
Thread-1:224
Thread-1:225
Thread-1:226
Thread-1:227
Thread-1:228
Thread-1:229
Thread-1:230
Thread-1:231
Thread-1:232
Thread-1:233
Thread-1:234
Thread-1:235
Thread-1:236
Thread-1:237
Thread-1:238
Thread-1:239
Thread-1:240
Thread-1:241
Thread-1:242
Thread-1:243
Thread-1:244
Thread-1:245
Thread-1:246
Thread-1:247
Thread-1:248
Thread-1:249
Thread-1:250
Thread-1:251
Thread-1:252
Thread-1:253
Thread-1:254
Thread-1:255
Thread-1:256
Thread-1:257
Thread-1:258
Thread-1:259
Thread-1:260
Thread-1:261
Thread-1:262
Thread-1:263
Thread-1:264
Thread-1:265
Thread-1:266
Thread-1:267
Thread-1:268
Thread-1:269
Thread-1:270
Thread-1:271
Thread-1:272
Thread-1:273
Thread-1:274
Thread-1:275
Thread-1:276
Thread-1:277
Thread-1:278
Thread-1:279
Thread-1:280
Thread-1:281
Thread-1:282
Thread-1:283
Thread-1:284
Thread-1:285
Thread-1:286
Thread-1:287
Thread-1:288
Thread-1:289
Thread-1:290
Thread-1:291
Thread-1:292
Thread-1:293
Thread-1:294
Thread-1:295
Thread-1:296
Thread-1:297
Thread-1:298
Thread-1:299

例子2解决问题的方法,是加上对象锁的方法解决问题,并将run()的执行代码部分,放在了加了对象锁的代码块中,从结果分析,线程的访问和修改不在穿插,调试多次没有问题。

那么在这个例子中,synchronized关键字怎么起作用的?也就是其工作原理是什么?

synchronized的工作原理是这样的:在java中,每个对象都有一个线程锁,synchronized可以用任意一个对象的线程锁来锁住一段代码,任何在进入该段代码的线程必须在解锁以后才能继续执行,否则就进入等待状态。其中,只有等占用该锁资源的线程执行完毕以后,该锁资源才会被释放。

 

 

这篇关于java多线程-用对象锁的方式控制线程同步的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!