Java Concurrency--Synchronized&& thread lock

I reviewed some concepts about thread again by reading the thinking in Java today. Here is some key points I want to note here. 


1) Why Synchronized is necessary?

When a program have two or more threads access to a shared recourse, there may be some conflict between threads. For example, 

//in thread 1
int sharedValue=0;
//********critical zone***********
sharedValue++; // thread maybe stop here
sharedValue++;
</pre><p></p><p>Now, suppose thread 2 intends to get the value of sharedValue (through some get method or read directly or whatever). Moreover, in this program, thread 2 are coded to process with even sharedValue. In that situation, problems occur. Maybe, when thread 1 just reach the first sharedValue++ and then be terminated or interrupted. Then sharedValue is a unexpected value for thread 2 and the program goes wrong. </p><p></p><p>the codes under the comments is a critical zone which should only access by one thread in one time. In java, the keyword <strong>synchronized </strong>is designed for that. </p><p>synchronized: apply for object, methods, classes and even code block {}. If something is labeled with that, this object,/value /methods,/classes / code block is not allowed to be access by multi- threads in the same time. </p><p></p><p>2) suggestions for the use of synchronized</p><p>a) if a value is labeled with synchronized, any methods can be access by other threads should also be synchronized. Same reason for synchronized method/ class</p><p>b) if synchronized is not used properly, programs only have chance to be wrong. In other word, it maybe seems OK because it only has a small chance to go wrong.</p><p>c) yield() increased that chance, some you can add some useless yield() to test your codes.</p><p></p><p>3) Lock</p><p>Besides synchronized, there is a less elegant way to deal with the critical zone. <strong>java.util.concurrent.locks.*;</strong></p><p>However, sometimes, the lock is more flexible. Here is a example codes from Thinking In Java"</p><p><pre name="code" class="java">int currentEvenValue=0;
Lock lock=new ReentranLock();
public int next(){
  lock.lock();
  try{
<span style="white-space:pre">	</span>currentEvenValue++;
<span style="white-space:pre">	</span>currentEvenValue++;
<span style="white-space:pre">	</span>return currentEvenValue;


  }finally{
<span style="white-space:pre">	</span>lock.unlock();
  }


}

You must place a try-finally statement with unlock() because it is the only way to ensure the unclock() is done.

In some situation, lock can not replaced by synchronized. For example, you want to lock for a certain time and release it. 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值