java - 我怎样才能让两个线程为我的应用程序工作

标签 java multithreading

我仍在学习 Java,目前我正在使用线程,所以基本上我正在尝试做一个我们有帐户的应用程序,现在我想使用两个不同的线程。一种是每 1 秒向账户中添加资金并显示余额,另一种是每 1 秒从账户中提取一定金额并显示结果余额。 所以我创建了我的 Account 类并因此反对

public class Account {
private double money;

public double getMoney() {
    return money;
}

public void setMoney(double money) {
    this.money = money;
}

Account (double money){
    this.money=money;
}}

我创建了两个类,一个用于添加,另一个用于撤回

public class Add extends Thread {
Account obj;
Add koi;
Add(Account obj){
    this.obj=obj;
}
public synchronized void add(Account obj, Add koi){
    this.obj=obj;
    this.koi=koi;
    while(obj.getMoney()<100){
double top= obj.getMoney()+10;
obj.setMoney(top);
System.out.println("The balance is : "+obj.getMoney());
try{
    Thread.sleep(1000);
    }
    catch(Exception e){
        System.out.println(e.toString());
    }}
}public void run(){
    add(obj,koi);}}



public class Withdraw extends Thread{
Account obj;
Withdraw koi;
Withdraw(Account obj){
    this.obj=obj;
}
public synchronized void with(Account obj, Withdraw koi){
    this.koi=koi;
    this.obj=obj;
    while (obj.getMoney()<100){
double top= obj.getMoney()-9;
obj.setMoney(top);
System.out.println("The balance is : "+obj.getMoney());
try{
    Thread.sleep(1000);
    }
    catch(Exception e){
        System.out.println(e.toString());
    }}
}public void run(){
  with(obj,koi);}}

这是主类:

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Account Steve= new Account(10);
    Add test= new Add(Steve);
    Withdraw test1= new Withdraw(Steve);
    test.start();
    test1.start();
    }}

问题是,当我运行程序时,add 方法完成了它的工作,但是当它显示输出时,结果不是我所期望的(调用 add 方法时余额为 10,它应该显示 20 但我得到 11,所以它显示的结果考虑了 withdraw 方法的结果,这不是我想要的)

最佳答案

(the balance is 10 when the add method is invoked it is supposed to display 20 but I'm getting 11 so the result that it displays take into account the result of the withdraw method and that's not what I want

这是因为两个线程并行工作,并且在显示添加结果之前运行 withdraw 方法。

我认为实现预期结果的唯一解决方案是同步共享对象上的函数:如下所示:

public void add(Account obj, Add koi){
    this.obj=obj;
    this.koi=koi;
    while(obj.getMoney()<100)
    {
        synchronized(obj)
        {
            double top= obj.getMoney()+10;
            obj.setMoney(top);
            System.out.println("The balance is : "+obj.getMoney());
        }
        try{
            Thread.sleep(1000);
        }
        catch(Exception e){
            System.out.println(e.toString());
        }
    }

}

和:

public void with(Account obj, Withdraw koi){
    this.koi=koi;
    this.obj=obj;
    while (obj.getMoney()<100)
    {
        synchronized(obj) 
        {
            double top= obj.getMoney()-9;
            obj.setMoney(top);
            System.out.println("The balance is : "+obj.getMoney());
        }
        try{
            Thread.sleep(1000);
        }
        catch(Exception e){
            System.out.println(e.toString());
        }
    }
}

synchrnozed(obj) 运算符锁定任何访问变量 obj 的线程,因此执行 block 内的所有操作,让其他线程等待

关于java - 我怎样才能让两个线程为我的应用程序工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37894559/

相关文章:

java - 用于 java 的 AES CS2Padding

c# - 如何跟踪 .Net 线程池的使用情况?

multithreading - 使用Spock测试线程并发

java - 如何在java中使用DefaultTableCellRenderer将图像插入到列表中

java - 如何将数据附加到 JTA 事务? (或唯一标识它)

java - Apache Tomcat 在 "Initializing started..."后挂起

java - Android 打开 URL onclick 某个按钮

C# 从静态类调用委托(delegate)

Java 多线程和安全发布

java - 如何使用 onRestart() 取消暂停/重启 Android Activity 中的线程?