Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.StringTokenizer;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.CountDownLatch;
- public class Bank {
- private static final int NUMBER_OF_ACCOUNTS = 20;
- private static final int DEFAULT_BALANCE = 1000;
- private static final int TRANSACTION_LIMIT = 30;
- private static int numberOfWorkers = 5; // for testing
- private static final String FILENAME = "small.txt"; // for testing
- private static Transaction nullTransaction;
- private static BlockingQueue<Transaction> queue;
- private static ArrayList<Account> accounts;
- private static ArrayList<Worker> workers;
- private static CountDownLatch latch;
- public Bank(String filename, String workerNum) {
- nullTransaction = new Transaction(-1, 0, 0);
- latch = new CountDownLatch(numberOfWorkers);
- numberOfWorkers = Integer.parseInt(workerNum);
- workers = new ArrayList<Worker>();
- accounts = new ArrayList<Account>();
- queue = new ArrayBlockingQueue<Transaction>(TRANSACTION_LIMIT);
- workerSetup();
- accountSetup();
- runThreads();
- readTransactions(filename);
- }
- /*
- * set up worker information
- */
- private void workerSetup() {
- for (int i = 0; i < numberOfWorkers; i++) {
- Worker w = new Worker();
- workers.add(w);
- }
- }
- /*
- * set up account information
- */
- private void accountSetup() {
- for (int i = 0; i < NUMBER_OF_ACCOUNTS; i++) {
- Account ac = new Account(i, DEFAULT_BALANCE);
- accounts.add(ac);
- }
- }
- /*
- * after account information is filled out we can run threads
- */
- private void runThreads() {
- for (int i = 0; i < numberOfWorkers; i++) {
- workers.get(i).start();
- }
- }
- public static String readFile(String filename) {
- String content = null;
- File file = new File(filename);
- FileReader reader = null;
- try {
- reader = new FileReader(file);
- content = new String(Files.readAllBytes(Paths.get(filename)));
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return content;
- }
- private static void readTransactions(String filename) {
- String file = readFile(filename);
- StringTokenizer st = new StringTokenizer(file);
- // it is guaranteed that (tokens % 3) = 0
- try {
- while (st.hasMoreTokens()) {
- int from = Integer.parseInt(st.nextToken());
- int where = Integer.parseInt(st.nextToken());
- int amount = Integer.parseInt(st.nextToken());
- Transaction trans = new Transaction(from, where, amount);
- queue.put(trans);
- }
- for (int i = 0; i < numberOfWorkers; i++) {
- queue.put(nullTransaction);
- }
- } catch (InterruptedException ex) {}
- }
- private void printAccountValues() {
- for (int i = 0; i < accounts.size(); i++) {
- System.out.println(accounts.get(i).toString());
- }
- }
- public static void main(String[] args) {
- String filename = args[0];
- String numberOfWorkers = args[1];
- Bank bank = new Bank(filename, numberOfWorkers);
- try {
- latch.await();
- } catch (InterruptedException ex) {}
- bank.printAccountValues();
- }
- /*
- * If they find a transaction they should process it. If the queue is empty,
- * they will wait for the Bank class to read in another transaction (you'll
- * get this behavior for free by using a BlockingQueue). Workers terminate
- * when all the transactions have been processed
- */
- public class Worker extends Thread {
- @Override
- public void run() {
- while (true) {
- try {
- Transaction tmp = queue.take();
- if (tmp == nullTransaction)
- break;
- else {
- // System.out.println("making transaction");
- accounts.get(tmp.getFrom()).process(tmp);
- accounts.get(tmp.getWhere()).process(new Transaction
- (tmp.getWhere(), tmp.getFrom(), -tmp.getAmount()));
- }
- } catch (Exception ignored) {
- ignored.printStackTrace();
- }
- }
- System.out.println(Thread.currentThread().getName() + " has finished");
- latch.countDown();
- }
- }
- }
Add Comment
Please, Sign In to add comment