标签:
package datatypeSmp;
public class threadtest {
public static void main(String[] args) {
salary sla = new salary();
new bossa(sla).start();
new bossb(sla).start();
}
}
class bossb extends Thread {
salary ss = null;
public bossb(salary s) {
this.ss = s;
}
public synchronized void run() {
for (int i = 0; i < 10; i++) {
ss.perf = (int) (Math.random() * 10);
ss.name="boss a ";
try {
sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (ss.perf > 5) {
ss.addsa();
} else {
ss.minsa();
}
}
}
}
class bossa extends Thread {
salary ss = null;
public bossa(salary s) {
this.ss = s;
}
public synchronized void run() {
for (int i = 0; i < 10; i++) {
ss.perf = (int) (Math.random() * 10);
ss.name="boss b ";
try {
sleep(3200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (ss.perf > 5) {
ss.addsa();
} else {
ss.minsa();
}
}
}
}
class salary {
boolean flag;
int sal = 0;
int perf = 0;// (int)(Math.random()*10);
String name;
public void addsa() {
this.sal += 1000;
System.out.println(name+" current salary is" + sal+" perfermance is" + perf);
}
public void minsa() {
this.sal -= 1000;
System.out.println(name+" current salary is" + sal+" perfermance is" + perf);
}
}
标签:
原文地址:http://www.cnblogs.com/JackDongHongyan/p/4833022.html