昨天看blog:http://blog.csdn.net/zyplus/article/details/6672775有这样一段话java
“在JAVA中,是没有相似于PV操做、进程互斥等相关的方法的。JAVA的进程同步是经过synchronized()来实现的,须要说明的是,JAVA的synchronized()方法相似于操做系统概念中的互斥内存块,在JAVA中的Object类型中,都是带有一个内存锁的,在有线程获取该内存锁后,其它线程没法访问该内存,从而实现JAVA中简单的同步、互斥操做。明白这个原理,就能理解为何synchronized(this)与synchronized(static XXX)的区别了,synchronized就是针对内存区块申请内存锁,this关键字表明类的一个对象,因此其内存锁是针对相同对象的互斥操做,而static成员属于类专有,其内存空间为该类全部成员共有,这就致使synchronized()对static成员加锁,至关于对类加锁,也就是在该类的全部成员间实现互斥,在同一时间只有一个线程可访问该类的实例。若是只是简单的想要实如今JAVA中的线程互斥,明白这些基本就已经够了。但若是须要在线程间相互唤醒的话就须要借助Object.wait(), Object.nofity()了。”网络
写的很是不错,这里画个图,补充个测试程序。ide
也就是分清楚到底锁住的是那一部份内存,也就是类和类的实例 内存关系。测试
测试程序以下:this
package com.jdcloud.xue.gang;spa
import java.util.concurrent.CountDownLatch;操作系统
import java.util.concurrent.ExecutorService;.net
import java.util.concurrent.Executors;线程
public class StaticMutiThread extends Thread{对象
public static final int SIZE=100;
//作个静态锁出来
static final Object LOCKER = new Object();
static int addint =0;
ExecutorService executor;
CountDownLatch countDownLatch;
public StaticMutiThread(ExecutorService executor,
CountDownLatch countDownLatch) {
super();
this.executor = executor;
this.countDownLatch = countDownLatch;
}
public void addPrint(){
synchronized (LOCKER) { //把这里的LOCKER换成this,来测试多个Thread同步试一试
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
e.printStackTrace();
}
addint +=1;
System.out.println("add int is :" + addint);
countDownLatch.countDown();
}
}// //锁住实例化出来的对象的内存。
public void run() {
addPrint();
}
class Runner{
public void runit(StaticMutiThread staticMutiThread){
executor.execute(staticMutiThread);
}
}
//测试Main方法
public static void main(String args[]) throws InterruptedException{
//用于关闭线程池
CountDownLatch countDownLatch = new CountDownLatch(SIZE);
//线程池
ExecutorService executor = Executors.newFixedThreadPool(SIZE);
for(int i =0;i<SIZE;i++){
//new一个线程出来
StaticMutiThread staticMutiThread = new StaticMutiThread(executor,countDownLatch);
//只是为了把线程加入到线程池
StaticMutiThread.Runner runner = staticMutiThread.new Runner();
runner.runit(staticMutiThread);
}
//运行直到关闭
countDownLatch.await();
executor.shutdown();
}
}
这段程序中生成了多个实例(多个this),若是在多个实例共有的静态区上加锁,能够协调多个实例,若是只在this上加上,则不行。
-----2014年3月17日
package com.zxg.test;
public class StaticClassTest{
//若是在方法上加上synchronized 至关于:synchronized (StaticClassTest.class)
//若是一个sync*方法锁住了一个方法没有释放锁,就会影响到其余方法。
//好比网络sync*方法中有网络链接,慢速攻击?
public static synchronized void pring_1(){
int i=10;
while(i>0){
System.out.println("---(1)-->"+i);
try {
Thread.currentThread().sleep(10L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i--;
}
}
public static void pring_2(){
synchronized (StaticClassTest.class) {
int i=10;
while(i>0){
System.out.println("---(2)-->"+i);
try {
Thread.currentThread().sleep(100L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i--;
}
}
}
public static void main(String args[]){
//test 1
new Thread(new Runnable(){
@Override
public void run() {
StaticClassTest.pring_2();
}
}).start();
//test 1
new Thread(new Runnable(){
@Override
public void run() {
StaticClassTest.pring_1();
}
}).start();
System.out.println("12312312");
}
}