刚看了一篇文章,在这里写一下我对synchronized块和synchronized方法的区别的理解。文章链接http://www.iteye.com/magazines/131-Java-Concurrency。
synchronized是用来解决同步时遇到的线程干扰和内存一致性错误问题的,假设我现在需要在多个线程中对一个数字进行加减,例子代码如下:
</pre><pre name="code" class="java">public class synchronizedTest1 {
private static int NUM_OF_THREAD = 1000;
static Thread[] threads = new Thread[NUM_OF_THREAD];
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final demo1 d = new demo1();
for (int i = 0; i < NUM_OF_THREAD; i++) {
threads[i] = new Thread(new Runnable() {
public void run() {
d.add();
d.del();
}
});
threads[i].start();
}
for (int i = 0; i < NUM_OF_THREAD; i++) {
try {
threads[i].join(); // 等待所有线程运行结束
} catch (InterruptedException e) {
System.out.println("threads[" + i + "] excetion: "
+ e.toString());
}
}
System.out.println(d);
}
}
class demo1 {
int count = 0;
public void add() {
count = count + 100;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println("inAccount excetion: " + e.toString());
}
}
public void del() {
count = count - 100;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println("inAccount excetion: " + e.toString());
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
return " count:" + count;
}
}
我对count执行了1000次的加减,理论上结果应该是0,可是实际输出结果却是不确定的。这是为什么呢? 前文所说的线程干扰和内存一致性错误在这里就体现了出来,我在每个线程中进行了加减,但是同时一千个线程进行运行,系统对时间片的分配却是随机的,可能线程A读取了count的值进行了加,count值进行了改变,但是同时线程B读取了count的值对count进行了减,count的值也发生了改变,二者将改变后的值返回给count后,有一个操作就被覆盖了,起因便是count数据同时被多个线程使用,每个线程都不能保证自己在使用count的过程中,别的线程不会也对count进行改变。各个线程之间的同时对数据进行操作,但是数据的改变不能及时的返回。
为了解决这样的问题,synchronized出现了,它就是确保被synchronized的方法在同一时间内只能有一个线程进行操作,这个线程操作完成返回后,别的线程才能进行操作。这样就能解决线程干扰和内存一致性的问题。将上面的代码修改如下:
public class synchronizedTest {
private static int NUM_OF_THREAD = 100;
static Thread[] threads = new Thread[NUM_OF_THREAD];
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final demo d = new demo();
for (int i = 0; i < NUM_OF_THREAD; i++) {
threads[i] = new Thread(new Runnable() {
public void run() {
d.add();
d.del();
}
});
threads[i].start();
}
for (int i = 0; i < NUM_OF_THREAD; i++) {
try {
threads[i].join(); // 等待所有线程运行结束
} catch (InterruptedException e) {
System.out.println("threads[" + i + "] excetion: "
+ e.toString());
}
}
System.out.println(d);
}
}
class demo {
int count = 0;
public synchronized void add() {
count = count + 100;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println("inAccount excetion: " + e.toString());
}
}
public synchronized void del() {
count = count - 100;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println("inAccount excetion: " + e.toString());
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
return " count:" + count;
}
}
在这里对方法进行了synchronized,运行结果就正确了。synchronized方法可以理解为在对象上加了一把锁,每个线程访问时别的线程就不能在对改对象的synchronized方法进行访问。
synchronized块是在代码块上加上了锁,
<span style="white-space:pre"> </span>public void test() {
synchronized(this){
//加锁代码块
}
}
上面的代码是在用this作为锁,和synchronized方法一样,都是锁住整个对象。但是synchronized块可以自己声明对象作为锁,下面举个例子
public class Foo {
private int val;
private int val1;
private static Object lock = new Object();
private static Object lock1 = new Object();
public void operateVal() {
synchronized (lock) {
val++;
}
}
public void operateVal1() {
synchronized (lock1) {
val1++;
}
}
}
在这段代码里自己声明了两个object对象,用作锁,因为在这里val和val1二者之间没有任何关联,如果操作其中一个时,将整个对象都锁住的话,那么效率就会低很多,在这里对代码块加上锁,访问该代码块的时候多线程会获取代码块的锁进行访问,保证了两个方法可以在保持同步的情况下互不干扰。更加的灵活方便。