java synchronized/wait/notify/互斥/同步

1.互斥问题

synchronized关键字,天生互斥,只用这个关键字就能实现互斥。两个线程有一个共同的锁lock,那么其中一个拿到lock,另一个就要等待。模型如下:

thread A:

synchronized(lock){

//do

}


thread B:

synchronized(lock){

//do

}

这两个线程的两个synchronized块中的代码是互斥的

代码如下:两个类SynchronizeTest / SynchronizeTest1

package spidercsdn;

public class SynchronizeTest implements Runnable{

	public Object lock = new Object();
	public static SynchronizeTest instance = null;
	
	public Object getLock(){
		return lock;
	}
	
	public static synchronized SynchronizeTest getInstance() {
	
		if(instance==null)
			instance = new SynchronizeTest();
		return instance;
	}
	
	public void setName() throws InterruptedException{
		synchronized(lock){
			System.out.println("lock inner");
			Thread.sleep(10000);
			System.out.println("after sleep,you can print");
		}
		Thread.sleep(10000);
		System.out.println("lock outter");
	}
	
	public static void main(String[] args) throws InterruptedException {
		new Thread(SynchronizeTest.getInstance()).start();
		new Thread(new SynchronizeTest1()).start();
	}

	@Override
	public void run() {
		try {
			SynchronizeTest.getInstance().setName();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

package spidercsdn;

public class SynchronizeTest1 implements Runnable{

	public void setName(){
		synchronized (SynchronizeTest.getInstance().getLock()) {
			System.out.println("inner st1");
		}
		System.out.println("outter st1");
	}
	
	public static void main(String[] args) {
		
	}

	@Override
	public void run() {
		SynchronizeTest1 st1 = new SynchronizeTest1();
		st1.setName();
	}
}

2.wait notify实现的同步问题

wait notify都要出现在synchronized语句块中,一旦wait,wait后面的语句无论在synchronized里面还是外面,一律全都不执行了,让出cpu。

一旦notify,notify后面的语句照常执行,并且通知wait的线程继续执行,但是,只有notify语句后面的在synchronized块中当语句执行完,wait的线程才继续,而notify语句后面的在synchronized块外的代码无所谓,没有特殊性,谁也不用等他执行完。

代码如下:两个类SynchronizeTest / SynchronizeTest1

package spidercsdn;

public class SynchronizeTest implements Runnable{

	public Object lock = new Object();
	public static SynchronizeTest instance = null;
	
	public Object getLock(){
		return lock;
	}
	
	public static synchronized SynchronizeTest getInstance() {
	
		if(instance==null)
			instance = new SynchronizeTest();
		return instance;
	}
	
	public void setName() throws InterruptedException{
		synchronized(lock){
			System.out.println("11111111");
			Thread.sleep(10000);
			System.out.println("22222222");
			lock.wait();
			System.out.println("55555555");
		}
		System.out.println("66666666");
	}
	
	public static void main(String[] args) throws InterruptedException {
		new Thread(SynchronizeTest.getInstance()).start();
		new Thread(new SynchronizeTest1()).start();
	}

	@Override
	public void run() {
		try {
			SynchronizeTest.getInstance().setName();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

package spidercsdn;

public class SynchronizeTest1 implements Runnable{

	public void setName() throws InterruptedException{
		synchronized (SynchronizeTest.getInstance().getLock()) {
			System.out.println("33333333");
			SynchronizeTest.getInstance().getLock().notify();
			Thread.sleep(10000);
			System.out.println("44444444");
		}
		Thread.sleep(10000);
		System.out.println("7777777");
	}

	@Override
	public void run() {
		SynchronizeTest1 st1 = new SynchronizeTest1();
		try {
			st1.setName();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值