Allove Team 致力于移动应用的开发和服务(Android,J2ME,iphone,ophone)
« »
2008年05月27日作者:孙建[技术分享]J2SE

6,496位读者

JAVA多线程编程:wait() 和 notify() 方法示例

一个简单的生产者和消费者的之间的产品流通示例,利用java多线程编程,产生生产者和消费者两个子线程访问同步方法。演示了wait()和notify()方法的基本含义 ^_^ 代码如下:

JAVA多线程编程:wait() 和 notify() 方法示例

下载: Counter.java
  1. class Counter {
  2. int n;
  3. boolean valueSet = false ;
  4. synchronized int get() {
  5. if (!valueSet)
  6. try{
  7. wait();
  8. }catch(InterruptedException e){
  9. System.out.println("InterruptedExcepitons Caught in Get Methord");
  10. }
  11.  
  12. System.out.println("Got: "+n);
  13. valueSet = false ;
  14. notify();
  15. return n;
  16. }

  1. synchronized void put(int n){
  2. if(valueSet)
  3. try{
  4. wait();
  5. }catch(InterruptedException e){
  6. System.out.println("InterruptedException Caught in Put Methord");
  7.  
  8. }
  9. this.n = n;
  10. valueSet = true ;
  11. System.out.println("Put: "+n);
  12. notify();
  13.  
  14. }
  15. }
  16.  
  17. class Producer implements Runnable {
  18.  
  19. Counter counter;
  20. Producer(Counter counter){
  21. this.counter = counter ;
  22. new Thread (this,"Producer").start();
  23. }
  24.  
  25. public void run(){
  26. int i = 0;
  27. while(true){
  28. counter.put(i++);
  29. }
  30. }
  31.  
  32. }
  33.  
  34. class Customer implements Runnable{
  35.  
  36. Counter counter ;
  37. public Customer(Counter counter){
  38. this.counter = counter ;
  39. new Thread(this,"Customer").start();
  40. //this.Thread.sleep(10000);
  41. }
  42. public void run (){
  43. while(true){
  44. counter.get();
  45. }
  46. }
  47. }
  48. public class ProducerAndCustomer {
  49. public static void main(String [] args){
  50. Counter counter = new Counter();
  51. new Producer(counter);
  52. new Customer(counter);
  53. System.out.println("haha");
  54. }
  55.  
  56. }

随机日志

日志信息 »

该日志于2008-05-27 23:18由 孙建 发表在[技术分享]J2SE分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

317条评论

  1. John1054 说:

    I bookmarked this guestbook. Thank you for good job!
    cheap , , online, , ,

    回复

  2. John1054 说:

    Great site. Good info
    , cheap , cheap , , ,

    回复

  3. John1054 说:

    Beautiful site!
    , buy , , buy , cheap ,

    回复

  4. Viagra 说:

    Viagra pharmacy viagra 639480 [URL=http://www.connotea.org/user/LowestViagraPrice]viagra online[/URL] ()ob http://www.connotea.org/user/LowestViagraPrice mhsvo

    回复

  5. Viagra 说:

    Viagra pharmacy viagra 4933 [URL=http://www.wikio.com/user/lowest_viagra_price/bio]viagra online[/URL] ; 8 http://www.wikio.com/user/lowest_viagra_price/bio bjxb

    回复

  6. Viagra 说:

    Viagra pharmacy viagra 613930 [URL=http://www.britney.com/us/blog/lowest-viagra-price-guaranteed-fast-and-discreet-shipping-worldwide-free-consultation]viagra online[/URL] ((;)8 http://www.britney.com/us/blog/lowest-viagra-price-guaranteed-fast-and-discreet-shipping-worldwide-free-consultation xk qkim

    回复

  7. Viagra 说:

    Viagra pharmacy viagra 65222 [URL=http://careers.directi.com/users/viewuserprofile.action?username=viagra]viagra online[/URL] )(;d http://careers.directi.com/users/viewuserprofile.action?username=viagra zlxw

    回复

  8. Viagra 说:

    Viagra pharmacy viagra 403890 [URL=http://www.healthcentral.com/sexual-health/c/326906/profile]viagra online[/URL] ;b http://www.healthcentral.com/sexual-health/c/326906/profile alti

    回复

  9. Cialis 说:

    Cialis pharmacy cialis 746948 [URL=http://www.xbox360achievements.org/forum/member.php?u=229790]cialis online[/URL] b 8 http://www.xbox360achievements.org/forum/member.php?u=229790 rpwfc

    回复

  10. Cialis 说:

    Cialis pharmacy cialis 62504 [URL=http://docs.codehaus.org/users/viewuserprofile.action?username=cialis-discount]cialis online[/URL] dd http://docs.codehaus.org/users/viewuserprofile.action?username=cialis-discount sjrl

    回复

  11. Viagra 说:

    Viagra pharmacy viagra 7004 [URL=http://www.connotea.org/user/LowestViagraPrice]viagra online[/URL] ;: 8)8 http://www.connotea.org/user/LowestViagraPrice fxchf

    回复

  12. Viagra 说:

    Viagra pharmacy viagra 1603 [URL=http://www.wikio.com/user/lowest_viagra_price/bio]viagra online[/URL] d:(; http://www.wikio.com/user/lowest_viagra_price/bio enaurv

    回复

  13. Viagra 说:

    Viagra pharmacy viagra 88577 [URL=http://www.healthcentral.com/sexual-health/c/326906/profile]viagra online[/URL] ( 8o: http://www.healthcentral.com/sexual-health/c/326906/profile ouxfix

    回复

  14. Cialis 说:

    Cialis pharmacy cialis 207481 [URL=http://www.xbox360achievements.org/forum/member.php?u=229790]cialis online[/URL] ;) () http://www.xbox360achievements.org/forum/member.php?u=229790 xgxsp

    回复

  15. Viagra 说:

    Viagra pharmacy viagra 797610 [URL=http://careers.directi.com/users/viewuserprofile.action?username=viagra]viagra online[/URL] (:(d http://careers.directi.com/users/viewuserprofile.action?username=viagra dlikof

    回复

  16. Viagra 说:

    Viagra pharmacy viagra 9852 [URL=http://www.britney.com/us/blog/lowest-viagra-price-guaranteed-fast-and-discreet-shipping-worldwide-free-consultation]viagra online[/URL] )( http://www.britney.com/us/blog/lowest-viagra-price-guaranteed-fast-and-discreet-shipping-worldwide-free-consultation kkjwrn

    回复

  17. Cialis 说:

    Cialis pharmacy cialis 264406 [URL=http://docs.codehaus.org/users/viewuserprofile.action?username=cialis-discount]cialis online[/URL] (d http://docs.codehaus.org/users/viewuserprofile.action?username=cialis-discount jaxf

    回复

发表评论 »

返回顶部