Java多线程学习四

Wait和Notify

wait:让线程进入等待状态。
notify: 唤醒线程,使线程不在处于等待状态。
当线程A运行的过程中遇到不满足的条件需要等待,等待线程B去更改系统的状态,当线程B改变了系统的状态,再次唤醒线程A,线程A查看是否满足条件,如果满足条件则继续执行,如果不满足条件则进入等待状态。

面试题:wait和sleep有什么区别?
1.sleep必须指定时间,wait可以指定也可以不指定。
2.sleep和wait都是让线程处于休眠状态,释放执行权
3.持有锁的线程执行sleep时是不释放锁的,持有锁的线程执行wait是释放锁的。
4.sleep时间到了自动苏醒,wait只能被notify进行唤醒。

下面是关于wait的一个实例:
通过wait,可以让三个线程交替运行。

public class WaitThread {
	PrintStr ps=new PrintStr();
	public static void main(String[] args) {
		new WaitThread();
	}
	public WaitThread(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(true) {
					ps.f1();
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(true) {
					ps.f2();
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(true) {
					ps.f3();
				}
			}
		}).start();
	}
}
class PrintStr{
	public int flag=1; 
	public synchronized void f1() {
		while(flag!=1) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("我是线程111111111");
		flag=2;
		notifyAll();
	}
	
	public synchronized void f2() {
		while(flag!=2) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("我是线程222222222");
		flag=3;
		notifyAll();
	}
	
	public synchronized void f3() {
		while(flag!=3) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("我是线程33333333");
		flag=1;
		notifyAll();
	}
}

多线程关于生产者和消费者问题

//餐厅的产品类
public class Product {
	private int id;
	private String name;
	
	
	public Product(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}

import java.util.LinkedList;
//餐厅的桌子类
public class Table {
	LinkedList<Product> productList = new LinkedList<>();
	public int max =10;
	
	public synchronized void put(Product p) {
		while(getSize()==max) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		productList.add(p);	
		notifyAll();
	}
	
	public synchronized Product take() {
		while(!(getSize()>0)) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		Product takePro=productList.removeLast();
		notifyAll();
		return takePro;
	}
	
	public int getSize() {
		return productList.size();
	}
	
	public void printSize() {
		System.err.println("桌子上的食物数量为:"+getSize());
	}
	
}
//消费者
public class Customer extends Thread{
	private Table table;
	
	public Producer(Table table) {
		super();
		this.table = table;
	}

	@Override
	public void run() {
		while(true) {
			Product p=table.take();
			System.out.println("消费者消费的产品的名字:"+p.getName()+"产品的id为:"+p.getId());
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
//生产者
public class Producer extends Thread{
	private Table table;	
	
	public Customer(Table table) {
		super();
		this.table = table;
	}

	@Override
	public void run() {
		int i=0;
		while(true) {
			if(i==Integer.MAX_VALUE) {
				i=0;
			}
			++i;
			Product p = new Product(i, "产品:"+i);
			table.put(p);
			System.out.println("生产者做了一个新产品,放在桌子上");
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
public class Go1 {
	public static void main(String[] args) {
		//确保是同一张桌子
		Table table = new Table();
		
		for(int i=0;i<5;i++) {
			Producer producer = new Producer(table);
			producer.start();
		}
		
		for(int i=0;i<3;i++) {
			Customer cust = new Customer(table);
			cust.start();	
		}
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(true) {
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					table.printSize();	
				}
	
			}
		}).start();
	}
}

多线程死锁:

所谓死锁:就是指两个或者两个以上的线程在执行的过程中,因为争抢资源,而造成相互等待的现象,如果没有外力的作用,他们将无法继续执行,此时系统成为死锁状态。

死锁产生的原因
1.系统资源不足
2.进程运行推进的顺序不正确
3.资源分配不当

产生死锁的条件
1.互斥条件:所谓互斥就是进程在某一段时间独占资源。
2.请求与保持条件:一个进程因为请求资源而阻塞时,对已获得的资源不释放。
3.不剥夺条件:进程已经获得的资源,在未使用完成前,不能强行剥夺。
4.循环等待条件:若干进程,形成一种头尾相连的循环等待资源的关系。

避免死锁:
上述四个条件,只要破坏其中一个即可避免死锁的产生。

下面我们来看一个死锁的实例:

public class DeathThread implements Runnable{
	private boolean flag;
	
	public DeathThread(boolean flag) {
		super();
		this.flag = flag;
	}

	@Override
	public void run() {
		if(flag) {
			synchronized(Test.LOCKA) {
				System.out.println("lock a haha");
				synchronized (Test.LOCKB) {
					System.out.println("lock b hehe");
				}
			}
		}else {
			synchronized(Test.LOCKB) {
				System.out.println("lock b hehe");
				synchronized (Test.LOCKA) {
					System.out.println("lock a haha");
				}
			}
		}
	}

}

class Test{
	public static Object LOCKA=new Object();
	public static Object LOCKB=new Object();
}
public class Go3 {
	public static void main(String[] args) {
		DeathThread d1=new DeathThread(true);
		DeathThread d2=new DeathThread(false);
		Thread t1 =new Thread(d1);
		Thread t2 =new Thread(d2);
		t1.start();
		t2.start();
	}
}

解析:多个线程因为相互等待而进入到死锁状态造成资源浪费。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值