当前位置: 首页 > 知识库问答 >
问题:

我的任务是创建一个Java应用程序,使用2个同步线程来计算每月利息并更新账户余额

晏富
2023-03-14

这是代码参考的参数。创建AccountSavings类。该类有两个实例变量:一个用于保持年利率的双变量和一个用于保持储蓄平衡的双变量。年利率为5.3,储蓄余额为100美元
•创建计算月利息的方法。•创建一个方法来运行两个线程。使用匿名类创建这些线程。第一个线程调用每月利息计算方法12次,然后显示储蓄余额(第12个月的余额)。之后,该线程Hibernate5秒。第二个线程调用每月利息计算方法12次,然后显示储蓄余额(第12个月的余额)。在主Thread结束之前,必须完成这两个Thread。•将main方法添加到同一个类中并测试线程。执行这两个线程后,节省的余额必须保持不变

在我的runThread方法中调用monthlyinterest方法时,我收到一个错误。非静态方法monthlyinterest()无法从静态上下文中引用,我似乎无法弄清楚如何解决这个问题。

...

import static java.lang.Thread.sleep;

class AccountSavings {

double annualInterest=5.3;
double savings=100.00;



public void monthlyInterest(){
double monthlyRate;
monthlyRate = annualInterest/12;
double balance = 0;
balance+=savings*monthlyRate;
}

public synchronized static void runThread(){

    Thread t1;
    t1 = new Thread(){
        AccountSavings accountSavings= new AccountSavings();
        @Override
        public void run(){
            for(int i=1;i<13;i++){
                System.out.println("Balance after " + i + "month: " + monthlyInterest());
            }
            try{sleep(5000);}
            catch(InterruptedException e){e.printStackTrace();}
        }
    };
Thread t2= new Thread(){

AccountSavings accountSavings=new AccountSavings();
@Override
public void run(){

    for(int i=1;i<13;i++){
    System.out.println("Balance after " + i + " month: " + monthlyInterest(balance));
    }
    try{sleep(5000);}
    catch(InterruptedException e){e.printStackTrace();}    
}
};
t1.start();
t2.start();

}

public static void main(String[] args){

    runThread();

}

}

共有2个答案

曾山
2023-03-14

首先,关键字同步必须转到将执行关键部分的方法,而不是将创建线程的方法。所以在这种情况下,它必须转到monthlyinterest。现在,您正在创建两个Account tSavings,它们只被一个线程修改,因此没有必要添加同步关键字,除非您希望通过多个线程修改它。此外,我建议使用Java8 lambda语法来创建线程,这更简单、更容易阅读。最后,您的代码看起来像下面的代码。

账户储蓄

public class AccountSavings {

    private double annualInterest;
    private double savings;
    private double balance;

    public synchronized double monthlyInterest() {
        double monthlyRate;
        monthlyRate = annualInterest / 12;
        balance += savings * monthlyRate;
        return balance;
    }

    AccountSavings(double annualInterest, double savings, double balance) {
        this.annualInterest = annualInterest;
        this.savings = savings;
        this.balance = balance;
    }

}

主要的

public class Main {

    public static void main(String args[]) {

        Thread t1 = new Thread(() -> {
            AccountSavings accountSavings = new AccountSavings(5.3, 100, 0);
            for (int i = 1; i < 13; i++) {
                System.out.println("Balance after " + i + "month: " + accountSavings.monthlyInterest() + " | Thread 1");
            }
            try {
                sleep(5000);
            } catch (Exception e) {
                System.out.println("Error sleeping");
            }
        });

        Thread t2 = new Thread(() -> {
            AccountSavings accountSavings = new AccountSavings(5.3, 100, 0);
            for (int i = 1; i < 13; i++) {
                System.out.println("Balance after " + i + "month: " + accountSavings.monthlyInterest() + " | Thread 2");
            }
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                System.out.println("Error sleeping");
            }
        });
        t1.start();
        t2.start();

    }

}
郭永怡
2023-03-14

编辑

从Account tSavings对象调用方法怎么样?帐户avings.montly兴趣()

class AccountSavings {

   private double annualInterest = 5.3;
   private double balance = 0;

   public void monthlyInterest() {
      double monthlyRate = annualInterest / 12;
      double savings = 100.00;
      balance += savings * monthlyRate;
   }

   public static void runThread() {

      Thread t1;
      t1 = new Thread() {
         AccountSavings accountSavings = new AccountSavings();
         @Override
         public void run() {
            int i=1;
            for (; i < 13; i++) {
               accountSavings.monthlyInterest();
            }
            System.out.println("Balance Thread 1 after " + (i-1) + " month: " + accountSavings.balance);
            try {
               sleep(5000);
            } catch (InterruptedException e) {
               // failed to sleep
            }
         }
      };
      Thread t2 = new Thread() {

         @Override
         public void run() {
            AccountSavings accountSavings = new AccountSavings();
            int i=1;
            for (; i < 13; i++) {
               accountSavings.monthlyInterest();
            }
            System.out.println("Balance Thread 2 after " + (i-1) + " month: " + accountSavings.balance);
            try {
               sleep(5000);
            } catch (InterruptedException e) {
              // failed to sleep
            }
         }
      };
      t1.start();
      t2.start();
   }

   public static void main(String[] args) {
      runThread();
   }
}
  1. 12个月后的余额线程1:5300000000000001
 类似资料:
  • 这是一个简单的场景: 用户从Web应用程序的网页触发一些操作。这个操作很重,需要更多的时间。 在操作在服务器端完成之前,用户触发器会用一些不同的参数表示相同的操作。因此,第二个请求的第二个操作也将开始处理。 在这种情况下,是否有两个不同的线程,比如第一个线程处理第一个请求,另一个线程处理第二个请求?或者它只是一个线程处理两个请求,第一个操作只是为了执行第二个请求而被中断(未完成)? 在这里,我不想

  • 所以我有一个任务来创建一个程序,它有一个起始余额为1的银行账户。然后,我创建了一组卡,可以访问该帐户、存款或取款,但不能同时进行。我创建了许多线程,然后使用这些线程访问银行帐户,提取或存入随机金额,然后退出。 我的卡片类别: 我的帐户类别: } 我的课程 } 输出示例: http://puu.sh/lvoE5/05ebcd4c74.png 正如你所看到的,它对每一张不同的卡都有效,它回到5000,

  • 所以我认为这里有2个场景: 每个地区(西欧除外)创建一个大型存储帐户,每个应用程序创建一个容器。存储帐户转到每个区域的资源组 1的退税: > 容器被设计为具有扁平结构(您可以创建文件夹,但这并不明显) 2的退税: 很多存储帐户 考虑到这一点,我想说为每个应用程序创建存储帐户是一个更好的选择,但也许我遗漏了一些东西。 所以问题是:在这里有其他的事情要比另一个更好的选择吗?也许是成本? 我偶然发现了一

  • 问题内容: 我是线程新手。我想创建一些与主线程分开工作的简单函数。但这似乎不起作用。我只想创建一个新线程,并在那里独立于主线程发生的事情做一些事情。这段代码看起来很怪异,但到目前为止,我对线程的了解还很少。你能解释一下这是怎么回事吗? 问题答案: 您正在线程方法中调用该方法。但是只有在线程已经启动时才调用该方法。改为这样做:

  • 本文向大家介绍clojure 创建一个新的应用程序,包括了clojure 创建一个新的应用程序的使用技巧和注意事项,需要的朋友参考一下 示例 按照上述说明并安装Leiningen后,请运行以下命令来启动新项目: 这将使用该<project-name>文件夹中的默认Leiningen模板设置一个Clojure项目。莱宁根有多个模板,这些模板会影响项目的结构。最常见的是使用的模板“ app”,它添加了