员工张三有两个主管,主管A和主管B经常会根据张三的业绩给张三调工资,用同步线程的方法! 跪求大佬帮帮忙

秋去春来.. 2019-04-22 09:29:54
...全文
1676 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39936465 2020-03-04
  • 打赏
  • 举报
回复 1
这样应该能看到运行结果,其他的自己在修改吧

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();
       
	}
qq_39936465 2020-03-04
  • 打赏
  • 举报
回复
引用 3 楼 WzjLsl 的回复:
为什么我跑不出来?
借鉴用啊,其实还可以添加一个进程来追加achieve值,或者把achieve改大。
山川0416 2020-03-04
  • 打赏
  • 举报
回复
为什么我跑不出来?
qq_39936465 2019-04-22
  • 打赏
  • 举报
回复
下面提供一个参考

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

}
hoyshit 2019-04-22
  • 打赏
  • 举报
回复
adjustment(A,zhang) 只要在执行这个方法的使用锁住,zhang的工作就行了。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧