adjustment(A,zhang) 只要在执行这个方法的使用锁住,zhang的工作就行了。
 

解决方案 »

  1.   

    下面提供一个参考public class Person {
    public String name;
    public double salary;
    public int achieve;
    public Person(String name, double salary,int achieve) {
    super();
    this.name = name;
    this.salary = salary;
    this.achieve=achieve;
    }
    public double getSalary() {
    return salary;
    }
    public void setSalary(double salary) {
    this.salary = salary;
    }

    public int getAchieve() {
    return achieve;
    }
    public void setAchieve(int achieve) {
    this.achieve = achieve;
    }
    public boolean achievement() {
    if(achieve>100) return true;
    else return false;
    }
    }
    public class Adjust implements Runnable {
    Person p = null; public Adjust(Person p) {
    super();
    this.p = p;
    } public synchronized void run() {
    while (true) {
    if (p.achievement()) {
    p.setSalary(p.getSalary() + 1000);
    p.setAchieve(0);
    } }
    }
    }
    public class test { public static void main(String[] args) {
    // TODO Auto-generated method stub
    Person p=new Person("zhangsan", 10000, 150);
    Adjust A=new Adjust(p);
    Adjust B=new Adjust(p);
    Thread tA=new Thread(A);
    Thread tB=new Thread(B);
    tA.start();
    tB.start();
    }}