提问人:Mike Mitchell 提问时间:10/28/2023 最后编辑:John KugelmanMike Mitchell 更新时间:10/28/2023 访问量:20
如何将变量“totalTransactions”添加到自身加上变量“this.transactions”,以便每个线程的总数结转?
How do I add a variable "totalTransactions" to itself plus the variable "this.transactions" so that the total carries over for each thread?
问:
如果我有一个变量,我想添加它的值加上每个线程的事务数。我将如何在此方法中做到这一点?private int totalTransactions = 0;
@Override
public void run()
{
Random r = new Random();
for(int i=0;i<this.transactions; i++)
{
int source=r.nextInt(this.accounts);
int amount = r.nextInt(500) + 1;
int dest;
do {dest = r.nextInt(this.accounts);} while (source == dest);
try
{
this.bank.transaction(source,dest,amount);
} catch(Exception e){}
}
this.bank.end(this.index); //note that this thread is finished
totalTransactions = totalTransactions + this.transactions;
}
答: 暂无答案
评论