快速业务通道

?ùì???3ì£oéú2ú??ó???·??? - ±à3ìè???í?

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-14

?ùì???3ì£oéú2ú??ó???·???

ê±??:2011-10-07 blogjava Liver

3??°?èê?£o

ò???2??a£?éú2ú???ú1¤3§à?éú2úá?2ú?·oó£???2ú?·′?·?μ?2??aà?£?2??a′?·?êyá?óD?T£?μ±?ú2?oó£?í£ ?1éú2ú£??±μ?óD??·?×???2ú?·??·?oó2??ìD?éú2ú£???·???′ó2??aà?ìáè?2ú?·£?μ±2??a??2?ê±£?í£?1??·?2ú ?·£??±μ?2??a?DóD2ú?·ê±£?2??ìD???·?2ú?·?£

′ú??μ?êμ??£¨μ÷????3ìsleepê±???éò?êμ??éú2ú?ù?èó???·??ù?èμ?2?í?£?£o

TestProduceAndConsumer.java

package com.nantian; import java.util.Random; public class TestProduceAndConsumer { ?????? public static void main(String[] args) { ?????????????? // ′′?¨ò???1¤3§???ó ?????????????? ProductFactory pf = new ProductFactory(); ?????????????? // ′′?¨ò???éú2ú??oíò?????·???£?′?μY1¤3§μ?òyó?£?±£?¤á???2ù×÷μ?ê?í?ò???1¤3§ ?????????????? Producer p = new Producer(pf); ?????????????? Consumer c = new Consumer(pf); ?????????????? // ???ˉá?????3ì ?????????????? p.start(); ?????????????? c.start(); ?????? } } // 2ú?·1¤3§ class ProductFactory { ?????? // product±íê?2??a ?????? private char[] product = { '' '', '' '', '' '', '' '', '' ''}; ?????? // flag±ê??2ú?·êyá? ?????? private int flag = 0; ?????? // éú2ú2ú?· ?????? public synchronized void produceProduct(char p) throws InterruptedException { ?????????????? // ?D??2ú?·ê?·??ú2?£?ò?±????¨ê?·??ìD?éú2ú ?????????????? if (flag == product.length) { ?????????????????????? this.wait(); ?????????????? } ?????????????? // μ±′ú???′DDμ??aà?£?ò??¨2?ê??ú2?×′ì? ?????????????? product[flag++] = p; ?????????????? // 2é?′′?ê±2??a×′ì?£¨?aà?2?ê?óúòμ?????-2?·?£? ?????????????? System.out.print(p + "±?éú2ú£?μ±?°2??a×′ì?£o"); ?????????????? for (char tmp : product) { ?????????????????????? System.out.print(tmp); ?????????????? } ?????????????? System.out.println(); ?????????????? // éú2ú·?·¨íê3é£?è?1?′??úμè′y?óáD?Dμ???3ì£?ó|????D? ?????????????? this.notifyAll(); ?????? } ?????? // ??·?2ú?· ?????? public synchronized char consumeProduct() throws InterruptedException { ?????????????? // ?D??2??aê?·???2?£?ò?±????¨ê?·???·?2ú?· ?????????????? if(flag == 0) { ?????????????????????? this.wait(); ?????????????? } ?????????????? // μ±′ú???′DDμ??aà?£?ò??¨2?ê???2?×′ì? ?????????????? char p = product[--flag]; product[flag]='' ''; ?????????????? // 2é?′′?ê±2??a×′ì?£¨?aà?2?ê?óúòμ?????-2?·?£? ?????????????? System.out.print(p + "±???·?£?μ±?°2??a×′ì?£o"); ?????????????? for(char tmp : product) { ?????????????????????? System.out.print(tmp); ?????????????? } ?????????????? System.out.println(); ?????????????? // ??·?·?·¨íê3é£?è?1?′??úμè′y?óáD?Dμ???3ì£?ó|????D? ?????????????? this.notifyAll(); ?????????????? return p; ?????? } } // éú2ú?? class Producer extends Thread { ?????? private P

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号