我正在创建一个模拟个人消费模式的多线程java应用程序
我的问题是,我不知道run()方法应该在多少个类中,或者应该在哪个类中,以及如何正确访问和使用Bank Account类。我也不确定如何使用timer类,我希望每秒钟代表一周。任何帮助都将不胜感激 这里是一个简单的例子 您有一个我正在创建一个模拟个人消费模式的多线程java应用程序,java,multithreading,timer,simulation,Java,Multithreading,Timer,Simulation,我的问题是,我不知道run()方法应该在多少个类中,或者应该在哪个类中,以及如何正确访问和使用Bank Account类。我也不确定如何使用timer类,我希望每秒钟代表一周。任何帮助都将不胜感激 这里是一个简单的例子 您有一个Balance类,它保存一个AtomicInteger,即当前余额 您有三种可运行项,收入、支出和报表——这些都是不言自明的 该方法的核心是main方法。这将使用ScheduleXectorService计划不同的收入事件以不同的比率发生。这些评论解释了什么是什么 最后,
Balance
类,它保存一个AtomicInteger
,即当前余额
您有三种可运行项,收入
、支出
和报表
——这些都是不言自明的
该方法的核心是main
方法。这将使用ScheduleXectorService
计划不同的收入事件以不同的比率发生。这些评论解释了什么是什么
最后,我们有一个任务,在一年后关闭整个地段-这可以很容易地改变或删除,在这种情况下,它将永远运行
您有间隔重复任务和延迟后运行一次的任务(关机任务)的示例
此实现没有错误处理-任务将以静默方式失败;但考虑到任务的简单性,不应该有任何理由让它们公平
如果要引入非整数值,可能需要使用BigDecimal
,因为需要手动同步
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package spendingsimulation;
import java.util.Random;
import java.util.Timer;
/**
*
* @author B00533474
*/
public class Consumables implements Runnable {
private int workingTime ;
private String typeOfUtility ;
private Random randGen ;
private int incomeAmt;
Consumables(String name, int amt, int time) {
typeOfUtility = name ;
randGen = new Random();
workingTime = randGen.nextInt(10000) ; // Up to 10 seconds
incomeAmt = amt;
}
@Override
public void run() {
Timer myTimer = new Timer();
}
}
为了解决这个问题,你做了什么?如果你告诉我们你到目前为止做了什么,我们会更愿意回答你的问题。(询问更好问题的有用链接:,)此外,您的问题有相当多的代码。为了更快地获得更好的帮助,请发布一个。我建议您先让银行帐户线程安全,然后再使用线程进行攻击。提示,
Integer
不是线程安全的。JVM不能保证写操作是原子的。此外,如果没有volatile
,其他线程也不太可能看到写入。非常感谢大家的帮助,我现在使用ScheduledExecutorService
以定时间隔运行应用程序。唯一的问题是应用程序运行了6分钟。它应该只运行52秒吗?我试过这个:@Boris_the_Spider)<代码>//52秒executorService.schedule后关机(新Runnable(){@Override public void run(){accountBalance.getBalance();executorService.shutdown();},52,TimeUnit.SECONDS);}代码>但它仍然运行了6分钟?任何帮助都将非常感谢更新您的代码-这可能与长时间运行的任务有关。
package spendingsimulation;
import java.lang.Thread ;
public class SpendMain {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Viewing expenditure");
// Incoming payments
Thread incomeS1 = new Thread(new Incomming("Wage Recieved", 2000, 1000) );
Thread incomeS2 = new Thread(new Incomming("Interest Recieved", 10, 4000) );
// Outgoing costs
Thread outgoingS1 = new Thread(new Consumables("Oil Bill", 250, 3000)) ;
Thread outgoingS2 = new Thread(new Consumables("Food Bill", 600, 1000)) ;
Thread outgoingS3 = new Thread(new Consumables("Electricity Bill", 50, 1000)) ;
Thread outgoingS4 = new Thread(new Consumables("Entertrainment Bill", 400, 1000)) ;
Thread outgoingS5 = new Thread(new Consumables("Shopping Bill", 200, 1000)) ;
System.out.println("Expenditure commencing") ;
// Get the threads going
//Incomming
incomeS1.start();
incomeS2.start();
// Outgoing
outgoingS1.start();
outgoingS2.start();
outgoingS3.start();
outgoingS4.start();
outgoingS5.start();
System.out.println("Expenditure is now underway.") ;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package spendingsimulation;
import java.util.Random;
/**
*
* @author B00533474
*/
public class Incomming implements Runnable {
//private int workingTime ;
private String typeOfUtility ;
// private Random randGen ;
//private int incomingAmt;
public Incomming(String name, int addAmount, int time){
typeOfUtility = name ;
// randGen = new Random();
//workingTime = randGen.nextInt(10000) ; // Up to 10 seconds
// outgoingAmt = amt;
}
//@Override
public void run()
{
try
{
System.out.println(typeOfUtility + " has come into the account");
System.out.println(" The balance on the account is now: ");
//this.getBalance();
Thread.sleep(1000) ;
} // End of try block
catch(InterruptedException ex)
{
System.out.println(typeOfUtility + " Terminated early") ;
} // End of Catch clause
System.out.println(typeOfUtility + " has finished") ;
} // End of method run()
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package spendingsimulation;
import java.util.Random;
import java.util.Timer;
/**
*
* @author B00533474
*/
public class Consumables implements Runnable {
private int workingTime ;
private String typeOfUtility ;
private Random randGen ;
private int incomeAmt;
Consumables(String name, int amt, int time) {
typeOfUtility = name ;
randGen = new Random();
workingTime = randGen.nextInt(10000) ; // Up to 10 seconds
incomeAmt = amt;
}
@Override
public void run() {
Timer myTimer = new Timer();
}
}
static final class Balance {
private final AtomicInteger balance;
public Balance(int initalBalance) {
this.balance = new AtomicInteger(initalBalance);
}
public void debit(final int amount) {
balance.addAndGet(-amount);
}
public void credit(final int amount) {
balance.addAndGet(amount);
}
public int balance() {
return balance.get();
}
@Override
public String toString() {
return "£" + balance.get();
}
}
static final class Income implements Runnable {
private final Balance balance;
private final int income;
public Income(Balance balance, int income) {
this.balance = balance;
this.income = income;
}
@Override
public void run() {
balance.credit(income);
}
}
static final class Expenditure implements Runnable {
private final Balance balance;
private final int cost;
public Expenditure(Balance balance, int cost) {
this.balance = balance;
this.cost = cost;
}
@Override
public void run() {
balance.debit(cost);
}
}
static final class Statement implements Runnable {
private final Balance balance;
public Statement(Balance balance) {
this.balance = balance;
}
@Override
public void run() {
System.out.println("Your balance this month is " + balance.toString());
}
}
public static void main(String[] args) throws ParseException {
final Balance balance = new Balance(1000);
final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
//print balance - monthly
executorService.scheduleAtFixedRate(new Statement(balance), 0, 4, TimeUnit.SECONDS);
//income - monthly
executorService.scheduleAtFixedRate(new Income(balance, 1000), 0, 4, TimeUnit.SECONDS);
//rent - monthly
executorService.scheduleAtFixedRate(new Expenditure(balance, 500), 4, 4, TimeUnit.SECONDS);
//food - weekly
executorService.scheduleAtFixedRate(new Expenditure(balance, 50), 1, 1, TimeUnit.SECONDS);
//shutdown after a year
executorService.schedule(new Runnable() {
@Override
public void run() {
executorService.shutdown();
}
}, 52, TimeUnit.SECONDS);
}