Demo entry 6361980

java homework

   

Submitted by anonymous on May 06, 2017 at 05:53
Language: Java. Code size: 3.0 kB.

package space.bobcheng.task10;

import java.util.ArrayList;
import java.util.Vector;

/**
 * Created by bob on 17-5-6.
 */
public class BankDemo {
    public static void main (String[] args) {
        Bank mybank = new Bank();
        mybank.start();
    }
}


class Bank implements Runnable{
    private Vector<Integer> clients = new Vector<>();
    private Thread bankThread = null;
    private ArrayList<Counter> counters = new ArrayList<>();
    public Bank() {
        counters.add(new Counter(this));
        counters.add(new Counter(this));
        counters.add(new Counter(this));//假设有3个柜台
    }

    public synchronized int getOneClient(){
        if(clients.isEmpty())
            return 0;
        else {
            int clientno = clients.get(0);
            clients.remove(0);
            return clientno;
        }
    }
    public void addOneClient(int clientNum){
        clients.add(clientNum);
    }

    public void start(){
        if(bankThread == null){
            bankThread = new Thread(this);
            bankThread.start();
            for(Counter counter : counters){
                counter.start();
            }
        }
    }

    public void stop(){
        for(Counter counter : counters){
            counter.stop();
        }
        bankThread = null;
    }

    @Override
    public void run() {
        int clientNo = 1;
        Thread cutThread = Thread.currentThread();
        while (cutThread == bankThread){
            int delay = (int) (Math.random() * 1000);
            try {
                this.addOneClient(clientNo++);
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
                this.stop();
            }

        }

    }
}

class Counter implements Runnable{
    private static int count = 0;
    private int number = 0;
    private Bank bank = null;
    private Thread counterThread = null;

    public Counter(Bank bank) {
        Counter.count ++;
        number = Counter.count;
        this.bank = bank;
    }

    public void start(){
        if(counterThread == null){
            counterThread = new Thread(this);
            counterThread.start();
        }
    }

    public void stop(){
        counterThread = null;
    }
    @Override
    public void run() {
        Thread currentThread = Thread.currentThread();
        while (currentThread == counterThread){
            int clientno = bank.getOneClient();
            if(clientno != 0){
                try {
                    System.out.println("Counter "+number+" starts to handle client " + clientno);
                    Thread.sleep((int)(Math.random() * 4000));
                    System.out.println("No."+ clientno +" client has finished");
                } catch (InterruptedException e) {
                    this.stop();
                    e.printStackTrace();
                }
            }
        }
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).