加载中…
个人资料
暗夜巡视
暗夜巡视
  • 博客等级:
  • 博客积分:0
  • 博客访问:13,814
  • 关注人气:32
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

java 例子1

(2012-01-08 10:52:56)
标签:

杂谈

分类: 计算机相关

要求写两个线程,一个线程将某个对象的某个成员变量的值加1,而另外一个线程将这个成员变量的值减1.使得该变量的值始终处于[0,2].初始值为0.

  1. package com.tju;  
  2. class Target  
  3.  
  4.     private int count;  
  5.       
  6.     public synchronized void increase()  
  7.      
  8.         if(count == 2 
  9.          
  10.             try  
  11.              
  12.                 wait();  
  13.               
  14.             catch (InterruptedException e)  
  15.              
  16.                 e.printStackTrace();  
  17.              
  18.          
  19.         count++;  
  20.         System.out.println(Thread.currentThread().getName() ":" count);  
  21.         notify();  
  22.      
  23.       
  24.     public synchronized void decrease()  
  25.      
  26.         if(count == 0 
  27.          
  28.             try  
  29.              
  30.                 //等待,由于Decrease线程调用的该方法,   
  31.                 //所以Decrease线程进入对象t(main函数中实例化的)的等待池,并且释放对象t的锁   
  32.                 wait();//Object类的方法   
  33.              
  34.             catch (InterruptedException e)  
  35.              
  36.                 e.printStackTrace();  
  37.              
  38.          
  39.         count--;  
  40.         System.out.println(Thread.currentThread().getName() ":" count);  
  41.           
  42.         //唤醒线程Increase,Increase线程从等待池到锁池   
  43.         notify();  
  44.      
  45.  
  46. class Increase extends Thread  
  47.  
  48.     private Target t;  
  49.       
  50.     public Increase(Target t)  
  51.      
  52.         this.t t;  
  53.      
  54.     @Override  
  55.     public void run()  
  56.         
  57.         for(int 0 ;i 30i++)  
  58.          
  59.             try  
  60.              
  61.                 Thread.sleep((long)(Math.random()*500));  
  62.              
  63.             catch (InterruptedException e)  
  64.              
  65.                 e.printStackTrace();  
  66.              
  67.               
  68.             t.increase();  
  69.          
  70.           
  71.      
  72.       
  73.  
  74. class Decrease extends Thread  
  75.  
  76.       
  77.     private Target t;  
  78.     public Decrease(Target t)  
  79.      
  80.         this.t t;  
  81.      
  82.       
  83.     @Override  
  84.     public void run()  
  85.      
  86.         for(int 0 30 i++)  
  87.          
  88.             try  
  89.              
  90.                 //随机睡眠0~500毫秒   
  91.                 //sleep方法的调用,不会释放对象t的锁   
  92.                 Thread.sleep((long)(Math.random()*500));  
  93.              
  94.             catch (InterruptedException e)  
  95.              
  96.                 e.printStackTrace();  
  97.              
  98.               
  99.             t.decrease();  
  100.               
  101.          
  102.           
  103.      
  104.       
  105.  
  106.   
  107. public class Test  
  108.  
  109.     public static void main(String[] args)  
  110.      
  111.         Target new Target();  
  112.           
  113.         Thread t1 new Increase(t);  
  114.         t1.setName("Increase");  
  115.         Thread t2 new Decrease(t);  
  116.         t2.setName("Decrease");  
  117.           
  118.         t1.start();  
  119.         t2.start();  
  120.      
  121.  

0

阅读 收藏 喜欢 打印举报/Report
前一篇:a good father
后一篇:romantic or not
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有