Java 12个线程每组两个访问同一个对象的void,synchronized,static void ,synchronized static void

先贴上测试代码

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无阻塞

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值