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();
}
}
}