先贴上测试代码
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SynchronizedTest {
public void a(int i) {
System.out.println("a"+" "+i);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----a"+" "+i);
}
public synchronized void b(int i) {
System.out.println("b"+" "+i);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("---b"+" "+i);
}
public synchronized void e(int i) {
System.out.println("e"+" "+i);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("---e"+" "+i);
}
public static void c(int i) {
System.out.println("c"+" "+i);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("---c"+" "+i);
}
public static synchronized void d(int i) {
System.out.println("d"+" "+i);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("---d"+" "+i);
}
public static synchronized void f(int i) {
System.out.println("f"+" "+i);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("---f"+" "+i);
}
public static void main(String[] argv) throws InterruptedException {
ExecutorService exec=Executors.newCachedThreadPool();
SynchronizedTest st=new SynchronizedTest();
exec.execute(new SynchronizedRunnableTest1(st,1));
exec.execute(new SynchronizedRunnableTest2(st,2));
exec.execute(new SynchronizedRunnableTest3(st,3));
exec.execute(new SynchronizedRunnableTest4(st,4));
exec.execute(new SynchronizedRunnableTest5(st,5));
exec.execute(new SynchronizedRunnableTest6(st,6));
exec.execute(new SynchronizedRunnableTest1(st,7));
exec.execute(new SynchronizedRunnableTest2(st,8));
exec.execute(new SynchronizedRunnableTest3(st,9));
exec.execute(new SynchronizedRunnableTest4(st,10));
exec.execute(new SynchronizedRunnableTest5(st,11));
exec.execute(new SynchronizedRunnableTest6(st,12));
exec.shutdown(); //注意关闭
}
}
class SynchronizedRunnableTest1 implements Runnable {
SynchronizedTest st=null;
int i;
public SynchronizedRunnableTest1(SynchronizedTest st, int i) {
super();
this.st = st;
this.i = i;
}
@Override
public void run() {
// TODO Auto-generated method stub
st.a(i);
}
}
class SynchronizedRunnableTest2 implements Runnable {
SynchronizedTest st=null;
int i;
public SynchronizedRunnableTest2(SynchronizedTest st, int i) {
super();
this.st = st;
this.i = i;
}
@Override
public void run() {
// TODO Auto-generated method stub
st.b(i);
}
}
class SynchronizedRunnableTest3 implements Runnable {
SynchronizedTest st=null;
int i;
public SynchronizedRunnableTest3(SynchronizedTest st, int i) {
super();
this.st = st;
this.i = i;
}
@Override
public void run() {
// TODO Auto-generated method stub
//调用两方法测试效果相同
//方法一
//SynchronizedTest.c(i);
//方法二
st.c(i);
}
}
class SynchronizedRunnableTest4 implements Runnable {
SynchronizedTest st=null;
int i;
public SynchronizedRunnableTest4(SynchronizedTest st, int i) {
super();
this.st = st;
this.i = i;
}
@Override
public void run() {
// TODO Auto-generated method stub
//SynchronizedTest.d(i);
st.d(i);
}
}
class SynchronizedRunnableTest5 implements Runnable {
SynchronizedTest st=null;
int i;
public SynchronizedRunnableTest5(SynchronizedTest st, int i) {
super();
this.st = st;
this.i = i;
}
@Override
public void run() {
// TODO Auto-generated method stub
//SynchronizedTest.d(i);
st.e(i);
}
}
class SynchronizedRunnableTest6 implements Runnable {
SynchronizedTest st=null;
int i;
public SynchronizedRunnableTest6(SynchronizedTest st, int i) {
super();
this.st = st;
this.i = i;
}
@Override
public void run() {
// TODO Auto-generated method stub
//SynchronizedTest.d(i);
st.f(i);
}
}
输出结果:
a 1
b 2
d 4
c 3
a 7
c 9
/* 停顿 */
----a 1
---b 2
e 11
---d 4
f 12
---c 3
----a 7
---c 9
/* 停顿 */
---e 11
e 5
---f 12
d 10
/* 停顿 */
---e 5
b 8
---d 10
f 6
/* 停顿 */
---b 8
---f 6
总结:有synchronized关键字的 void和static void 函数之间不会发生阻塞,即b,e之间会阻塞,d,f之间会阻塞,e与d、f无阻塞、b与d,f无阻塞,d与b、e无阻塞、f与b,e无阻塞