大家好.我是一个初学者.在调试程序的时候总回有这样的问题!我下面就举个简单的例子!请诸位学长帮帮我!在这里谢谢了!
这是一个书上的例子
/**
* this program shows multiple threads can safely access a data structure ,using
* sysnchsonized methods
*/
public class SynchBankTest2 {
public static void main(String[] args) {
Bank b = new Bank(100,1000);
for(int i = 0; i < 100; i++)
{
TransferRunnable r = new TransferRunnable(b,i,1000);
Thread t = new Thread(r);
t.start();
}
}
}
class Bank {
/**
* Construct the bank
*/
public Bank(int n, double initialBalance) {
accounts = new double[n];
for(int i = 0; i < accounts.length; i++)
{
accounts[i] = initialBalance;
}
}
/**
* Tranfers money from one account to another
*/
public synchronized void transfer(int from, int to, double amount) throws InterruptedException {
while(accounts[from] < amount)
wait();
System.out.print(Thread.currentThread());
accounts[from] -= amount;
System.out.printf(" %10.2 from %d to %d", amount,from, to);
accounts[to] += amount;
System.out.printf(" Total Balance: %10.2f%n",getTotalBalance());
notifyAll();
}
/**
* Gets the sum of all account balances
*/
public synchronized double getTotalBalance() {
double sum = 0;
for(double a : accounts)
{
sum += a;
}
return sum;
}
/**
* Gets the number of accounts int the bank
* return the number of accounts
*/
public int size() {
return accounts.length;
}
private final double[] accounts;
}
/**
* A runnable that tranfers money from an account to other
*/
class TransferRunnable implements Runnable {
/**
*Constuncts a transfer runnable
*/
public TransferRunnable(Bank b, int from, double max) {
bank = b;
fromAccount = from;
maxAmount = max;
}
public void run() {
try
{
while(true)
{
int toAccount = (int) (bank.size() * Math.random());
double amount = maxAmount * Math.random();
bank.transfer(fromAccount,toAccount,amount);
Thread.sleep((int) (DELAY * Math.random()));
}
}
catch (InterruptedException e)
{
}
}
private Bank bank;
private int fromAccount;
private double maxAmount;
private int repetitions;
private int DELAY = 10;
}
调试的时候出现这样的错误提示!
Thread[Thread-1,5,main]Exception in thread "Thread-1" java.util.UnknownFormatConversionException: Conversion = '1'
at java.util.Formatter.checkText(Formatter.java:2500)
at java.util.Formatter.parse(Formatter.java:2464)
at java.util.Formatter.format(Formatter.java:2411)
at java.io.PrintStream.format(PrintStream.java:899)
at java.io.PrintStream.printf(PrintStream.java:800)
at Bank.transfer(SynchBankTest2.java:39)
at TransferRunnable.run(SynchBankTest2.java:86)
at java.lang.Thread.run(Thread.java:595)
类似的问题出现了很多次
我们初学者遇到这样问题怎么样解决呢?