队列02:应用

来源:互联网 发布:氮素网络是什么意思 编辑:程序博客网 时间:2024/05/21 09:36

1 使用多线程实现排队买票

package com.stack;/** * 排队买票 * Created by weideliang on 2016/10/17. */public class WaitToBuyTicket implements Runnable {    private QueueApplication01 queueApplication01;    public WaitToBuyTicket(QueueApplication01 queueApplication01) {        this.queueApplication01 = queueApplication01;    }    @Override    public void run() {        while (queueApplication01.count < queueApplication01.Sell_Sum){            try {                queueApplication01.waitToBugTicket();            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }}
package com.stack;/** * 买票 * Created by weideliang on 2016/10/17. */public class SellTicket implements Runnable{    private QueueApplication01 queueApplication01;    public SellTicket(QueueApplication01 queueApplication01) {        this.queueApplication01 = queueApplication01;    }    @Override    public void run() {        while(queueApplication01.isAlive){            try {                queueApplication01.sellTicket();            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }}
package com.stack;/** * 队列应用:使用多线程实现排队买票 * 卖票窗口 每天只卖100张票 * 利用队列实现排队功能 * Created by Edward on 2016/10/16. */public class QueueApplication01 {    public Queue02<Integer> queue02 = new Queue02<Integer>();    public Integer Sell_Sum = 10;    public Integer count = 0;    public boolean isAlive = true;    /**     * 排队买票:入队     */    public synchronized void waitToBugTicket() throws InterruptedException {        if(count<Sell_Sum){            System.out.println("第"+(count+1)+"个人排队等待买票");            queue02.push(count++);            this.notifyAll();        }else {            System.out.println("今天的票已卖完,请明天再买");            this.wait();        }    }    public synchronized void sellTicket() throws InterruptedException {        if(!queue02.isEmpty()){            Integer count = queue02.pop();            System.out.println("第"+(count+1)+"个人已买票");            this.notifyAll();        }else{            System.out.println("今天的票已卖完,请明天再买!!!");            isAlive = false;            this.wait();        }    }    public static void main(String[] args) {        QueueApplication01 queueApplication01 = new QueueApplication01();        WaitToBuyTicket waitToBuyTicket = new WaitToBuyTicket(queueApplication01);        SellTicket sellTicket = new SellTicket(queueApplication01);        Thread waitThread = new Thread(waitToBuyTicket);        Thread sellThread = new Thread(sellTicket);        waitThread.start();        sellThread.start();    }}
2 字符串回文
package com.stack;/** * 字符串回文:如noon level 正着读和反着读都一样 */public class QueueApplication02 {    private Queue02<String> queue02 = new Queue02<String>();    private Stack01<String> stack01 = new Stack01<String>();    /**     * 判断是否是回文字符串     * @param str     * @return     */    public boolean isWhoops(String str){        String[] strings = StackApplicatio02.strToStrArray(str);        stack01.pushAll(strings);        queue02.pushAll(strings);        for (int i=0; i<strings.length; i++){            String stackPop = stack01.pop();            String queuePop = queue02.pop();            if(!stackPop.equals(queuePop)){                return false;            }        }        return true;    }    public static void main(String[] args) {        QueueApplication02 queueApplication02 = new QueueApplication02();        System.out.println(queueApplication02.isWhoops("noon"));        System.out.println(queueApplication02.isWhoops("noon1"));    }}
 
0 0
原创粉丝点击