Home > Back-end >  Zhang SAN has two competent employees, competent A and B are often zhang SAN's wages, according
Zhang SAN has two competent employees, competent A and B are often zhang SAN's wages, according

Time:10-17

CodePudding user response:

Adjustment (A, zhang) as long as using this method in the implementation of the lock, zhang work went,

CodePudding user response:

The following provides a reference
 
Public class Person {
public String name;
Public double salary;
Public int your;
Public Person (String name, double salary, int your) {
super();
this.name=name;
This. Salary=salary;
This. Your=your;
}
Public double getSalary () {
return salary;
}
Public void setSalary (double salary) {
This. Salary=salary;
}

Public int getAchieve () {
Return your;
}
Public void setAchieve (int your) {
This. Your=your;
}
Public Boolean achievement () {
If (achieve> 100) return true;
The else return false;
}
}

 
Public class Adjust implements Runnable {
Person p=null;

Public Adjust (Person) p {
super();
This. P=p;
}

Public synchronized void the run () {
While (true) {
If (p.a chievement ()) {
P. etSalary (p.g etSalary () + 1000);
P. etAchieve (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 ();
}

}

CodePudding user response:

Why couldn't I run?

CodePudding user response:

The
reference 3 floor WzjLsl response:
why couldn't I run?


Reference to use ah, in fact you can also add a process to increase your value, or put your change is big,

CodePudding user response:

This should be able to see the results, the other in change yourself

 
Public class Person {
private String name;
Private double salary;
Private int your;

Public Person (String name, double salary, int your) {
super();
this.name=name;
This. Salary=salary;
This. Your=your;
}

Public String getName () {
return name;
}

Public void elegantly-named setName (String name) {
this.name=name;
}

Public double getSalary () {
return salary;
}

Public void setSalary (double salary) {
This. Salary=salary;
}

Public int getAchieve () {
Return your;
}

Public void setAchieve (int your) {
This. Your=your;
}
}



 
Public class Adjust implements Runnable {
Person p=null;

Public Adjust (Person) p {
super();
This. P=p;
}

Public synchronized void the run () {
While (p.g etAchieve () & gt; 0 {
P. etSalary (p.g etSalary () + 1000);
System. The out. Println (" head "+ Thread. CurrentThread (). The getName () +" to "+ p.g etName () +" raise "+ 1000 + p.g etName () +" pay now "+ p.g etSalary ());
P. etAchieve (p.g etAchieve () - 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 ();

}

  • Related