0 0

用java来模拟观光塔问题10

假定有一座观光塔,只有一部电梯,只能容纳一个游客上观光塔或下观光塔。观光塔观光平台只能最多容纳10个人,请用java模拟这个情况。
各位该怎么写
问题补充
Rojeck 写道
生产者消费者问题
推荐新容器Blockingqueue

Blockingqueue就是一个栈吧
2010年8月25日 13:47

2个答案 按时间排序 按投票排序

0 0

各位批评批评 感谢

public class TestElevator {

    public static void main(String[] args) {
        Visitor jobs = new Visitor("KDS_001", "Jobs");
        Visitor gates = new Visitor("KDS_002", "Gates");
        Visitor jerry = new Visitor("KDS_003", "Jerry");
        Visitor tom = new Visitor("KDS_004", "Tom");
        Visitor chalie = new Visitor("KDS_005", "Chalie");

        Elevator elevator = Elevator.getInstance();
        elevator.rise(jobs);
        elevator.rise(gates);
        elevator.rise(jerry);
        elevator.rise(tom);
        elevator.rise(chalie);

        elevator.down();
        elevator.down();
        elevator.down();
        elevator.down();
    }
}




 
/*
 * @(#)TowerController.java    Aug 20, 2010
 *
 * Copyright 2009 Nyber(Shanghai Itd. All rights reserved.
 */

package com.lab.puzzle;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.lang.StringUtils;

/**
 * 假定有一座观光塔, 只有一部电梯, 只能容纳一个游客上观光塔或下观光塔. 观光塔观光平台只能最多容纳5个人.
 *
 * @author nickevin@gmail.com
 * @version Aug 20, 2010 5:37:34 PM
 */

public class Elevator {

    private final static Elevator instance = new Elevator();

    private final static int THRESHOLD = 5;
    private final static BlockingQueue<Visitor> tower = new LinkedBlockingQueue<Visitor>(
            THRESHOLD);
    ExecutorService service = Executors.newCachedThreadPool();

    private Elevator() {}

    public static Elevator getInstance() {
        return instance;
    }

    public void rise(Visitor visitor) {
        service.submit(new GetUpThread(tower, visitor));
    }

    public void down() {
        service.submit(new GetOffThread(tower));
    }
}

class GetUpThread implements Runnable {

    private final BlockingQueue<Visitor> tower;
    private Visitor visitor;

    GetUpThread(BlockingQueue<Visitor> tower, Visitor visitor) {
        this.tower = tower;
        this.visitor = visitor;
    }

    public void run() {
        try {
            while (true) {
                System.out.println(StringUtils.center(
                        "当前塔上人数: " + tower.size(), 80, "-"));
                tower.put(produce());
                System.out.println(visitor + " 正在上塔, 请注意安全.");

                Thread.sleep(1000);
            }
        } catch (InterruptedException ex) {
        }
    }

    private Visitor produce() {
        return visitor;
    }

}

class GetOffThread implements Runnable {

    private final BlockingQueue<Visitor> tower;

    GetOffThread(BlockingQueue<Visitor> tower) {
        this.tower = tower;
    }

    public void run() {
        try {
            while (true) {
                System.out.println(StringUtils.center(
                        "当前塔上人数: " + tower.size(), 80, "-"));
                consume(tower.take());

                Thread.sleep(1000);
            }
        } catch (InterruptedException ex) {
        }
    }

    private void consume(Visitor visitor) {
        System.out.println(visitor + " 正在下塔, 请注意安全.");
    }

}

class Visitor {

    private String cardId;
    private String name;

    public Visitor(String cardId, String name) {
        this.cardId = cardId;
        this.name = name;
    }

    @Override
    public int hashCode() {
       ...
    }

    @Override
    public boolean equals(Object obj) {
        ...
    }

    @Override
    public String toString() {
        return new StringBuilder("游客: " + name).toString();
    }

}



2010年8月25日 13:47
0 0

生产者消费者问题
推荐新容器Blockingqueue

2010年8月25日 13:47

相关推荐

Global site tag (gtag.js) - Google Analytics