uj5u.com熱心網友回復:
adjustment(A,zhang) 只要在執行這個方法的使用鎖住,zhang的作業就行了。uj5u.com熱心網友回復:
下面提供一個參考
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();
}
}
uj5u.com熱心網友回復:
為什么我跑不出來?uj5u.com熱心網友回復:
借鑒用啊,其實還可以添加一個行程來追加achieve值,或者把achieve改大。
uj5u.com熱心網友回復:
這樣應該能看到運行結果,其他的自己在修改吧
public class Person {
private String name;
private double salary;
private int achieve;
public Person(String name, double salary, int achieve) {
super();
this.name = name;
this.salary = salary;
this.achieve = achieve;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 class Adjust implements Runnable {
Person p = null;
public Adjust(Person p) {
super();
this.p = p;
}
public synchronized void run() {
while (p.getAchieve()>0) {
p.setSalary(p.getSalary() + 1000);
System.out.println("主管"+Thread.currentThread().getName()+"給"+p.getName()+"加薪"+1000+p.getName()+"現在工資"+p.getSalary());
p.setAchieve(p.getAchieve()-100);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p=new Person("zhangsan", 10000, 1500);
Adjust A=new Adjust(p);
Adjust B=new Adjust(p);
Thread tA=new Thread(A, "A");
Thread tB=new Thread(B,"B");
tA.start();
tB.start();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/130195.html
標籤:Java SE
