线程的安全问题级锁

package it.cast.threadtest;
public class CustomerAndProductor {
	public static void main(String[] args) {
		Person person = new Person();
		new Thread(new Productor(person)).start();
		new Thread(new Customer(person)).start();
	
	}
}
class Productor implements Runnable{
	private Person person = null;
	public Productor(Person person) {
		this.person = person;
	}
	@Override
	public void run() {
		int x = 0;
		while (true) {
			if (x == 0) {
				person.name = "赖龙威";
				person.sex = "男";
			}
			else {
				person.name = "lailongwei";
				person.sex = "man";
			}
			x = ((x + 1) % 2);
		}
		
	}
	
}

class Customer implements Runnable{
	private Person person;
	public Customer(Person person) {
		this.person = person;
	}
	@Override
	public void run() {
		while (true) {
			System.out.println(person.name + " : " + person.sex);
			System.out.println("------------------------------");
		}
		
	}
}

class Person {
	String name;
	String sex;
/*	public Person(String name, String sex) {
		this.name = name;
		this.sex = sex;
	}*/
	
}

解决的办法:

package it.cast.threadtest;
public class CustomerAndProductor {
	public static void main(String[] args) {
		Person person = new Person();
		new Thread(new Productor(person)).start();
		new Thread(new Customer(person)).start();
	
	}
}
class Productor implements Runnable{
	private Person person = null;
	public Productor(Person person) {
		this.person = person;
	}
	@Override
	public void run() {
		int x = 0;
		while (true) {
			synchronized (person) {
				
				if (x == 0) {
					person.name = "赖龙威";
					person.sex = "男";
				}
				else {
					person.name = "lailongwei";
					person.sex = "man";
				}
				x = ((x + 1) % 2);
			}
			}
		
	}
	
}

class Customer implements Runnable{
	private Person person;
	public Customer(Person person) {
		this.person = person;
	}
	@Override
	public void run() {
		while (true) {
			synchronized (person) {                //因为person是对同一个对象的应用.
				
				System.out.println(person.name + " : " + person.sex);
			}
			System.out.println("------------------------------");
		}
		
	}
}

class Person {
	String name;
	String sex;
/*	public Person(String name, String sex) {
		this.name = name;
		this.sex = sex;
	}*/
	
}


 线程之间添加通信.通信的方法有wait(),notify(),notifyAll();这些方法都来至于Object。因为Object对象就可以当作锁。通信是锁内的通信。只有在持有锁的情况下才可以调用哪些方法,不然会跑出异常.也就是这些方法要放到同步方法中,或者同步代码块中.wait()方法会放弃对这个锁的持有权。notify不会,notify容易造成死等。notifyAll会唤醒这个锁上的所有线程。

package it.cast.threadtest;
public class CustomerAndProductor {
	public static void main(String[] args) {
		Person person = new Person();
		new Thread(new Productor(person)).start();
		new Thread(new Customer(person)).start();
	
	}
}
class Productor implements Runnable{
	private Person person = null;
	public Productor(Person person) {
		this.person = person;
	}
	@Override
	public void run() {
		int x = 0;
		while (true) {
			synchronized (person) {
				while (person.flag) {
					try {
						person.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				if (x == 0) {
					person.name = "赖龙威";
					person.sex = "男";
				}
				else {
					person.name = "lailongwei";
					person.sex = "man";
				}
				x = ((x + 1) % 2);
				person.flag = !person.flag;
				person.notifyAll();
			}
			}
		
	}
	
}

class Customer implements Runnable{
	private Person person;
	public Customer(Person person) {
		this.person = person;
	}
	@Override
	public void run() {
		while (true) {
			synchronized (person) {
				while (!person.flag) {
					try {
						person.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				System.out.println(person.name + " : " + person.sex);
				System.out.println("------------------------------");
				person.flag = !person.flag;
				person.notifyAll();
			}
		}
		
	}
}

class Person {
	String name;
	String sex;
	boolean flag = false;
/*	public Person(String name, String sex) {
		this.name = name;
		this.sex = sex;
	}*/
	
}


正是因为会唤醒所有的线程,有时不是我们想要的线程,效率就比较低。

在jdk1.5中,Lock替代了synchronized方法和语句的使用。Condition替代了Object监视器方法的使用。因为Condition是来源于Lock的,所以要用lock的getConditon方法来得到。await()方法会跑出异常,所以要用到try{}catch{}语句块。这里需要注意的是,lock.unlock()要放到finally里面

package it.cast.threadtest;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class CustomerAndProductor {
	public static void main(String[] args) {
		Person person = new Person();
		new Thread(new Productor(person)).start();
		new Thread(new Customer(person)).start();
	
	}
}
class Productor implements Runnable{
	private Person person = null;

	public Productor(Person person) {
		this.person = person;
	}
	@Override
	public void run() {
		int x = 0;
		while (true) {
				if (x == 0) {
					person.setPerson("赖龙威", "男");
				}
				else {
					person.setPerson("lailongwei", "man");
				}
				x = (x + 1) % 2;
			}
		
	}
	
}

class Customer implements Runnable{
	private Person person;
	public Customer(Person person) {
		this.person = person;
	}
	@Override
	public void run() {
		while (true) 
			System.out.println(person.toString());
	}
}

class Person {
	String name;
	String sex;
	boolean flag = false;
	ReentrantLock lock = new ReentrantLock();
	Condition notFull = lock.newCondition();
	Condition notEmpty = lock.newCondition();
/*	public Person(String name, String sex) {
		this.name = name;
		this.sex = sex;
	}*/
	public void setPerson(String name, String sex) {
		try {
			lock.lock();
			while (!flag) {
				notEmpty.await();
			}
			this.name = name;
			this.sex = sex;
			flag = !flag;
			notFull.signalAll();
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally{
			lock.unlock();
		}
	}
	
	@Override
	public String toString() {
		try {
			lock.lock();
			if (flag) {
				notFull.await();
			}
			flag = !flag;
			notEmpty.signal();
			return name + " : " + sex;
		} catch (Exception e) {
			// TODO: handle exception
			return e.toString();
		}
		finally{
			lock.unlock();
		}
	}
	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值