zoukankan      html  css  js  c++  java
  • 利用多线程实现并发调用,实现一个存钱多人取钱函数

      1 package com.thread;
      2 
      3 /**
      4  * @author lisj
      5  * 利用多线程实现并发调用
      6  * 根据条件实现线程的等待和运行
      7  */
      8 
      9 public class ThreadMoney {
     10 
     11     public static void main(String args[]){
     12         
     13         Moneyclass money=new Moneyclass();    //实例化Money类 
     14         getmoney son1=new getmoney(money); //实例化线程进行取钱
     15         getmoney son2=new getmoney(money);
     16         getmoney son3=new getmoney(money);
     17         putmoney father=new putmoney(money);//实例化线程进行存钱
     18         son1.setName("son1");    //线程取名
     19         son2.setName("son2");
     20         son3.setName("son3");
     21         father.setName("father");
     22         
     23         son1.start();    //线程运行
     24         son2.start();
     25         son3.start();
     26         father.start();        
     27       }
     28         
     29     }
     30 
     31 
     32     /**
     33      * @author lisj
     34      * 设置存钱函数和取钱函数
     35      */
     36     class Moneyclass{        //设置金钱数
     37         
     38         int money=0;
     39         
     40         synchronized void put(){    //负责存钱方法
     41         
     42             if(money<100){    //钱不够就存钱
     43                 money=money+100;
     44                 System.out.println(Thread.currentThread().getName()+"存入100元"+"现在money="+money);
     45             }else{
     46                 try {
     47                     wait();        //钱够就等待
     48                 } catch (InterruptedException e) {
     49                     // TODO Auto-generated catch block
     50                     e.printStackTrace();
     51                 }
     52             }
     53             
     54             notify();    //唤醒其他线程
     55                 
     56         }
     57         
     58         
     59         synchronized void get(int n){    //取钱方法 传入取钱数
     60             
     61             if(money<n){    //余额不足 等待线程
     62                 try {
     63                     wait();
     64                 } catch (InterruptedException e) {
     65                     // TODO Auto-generated catch block
     66                     e.printStackTrace();
     67                 }
     68                 
     69             }else{        //余额充足就取钱
     70                 money=money-n;
     71                 System.out.println(Thread.currentThread().getName()+"取出"+n+"元,"+"现在money="+money);
     72             }
     73             
     74             notify();    //唤醒其他线程
     75                 
     76         }
     77         
     78         
     79     }
     80 
     81 
     82     /**
     83      * @author lisj
     84      * 设置取钱线程
     85      */
     86     class getmoney extends Thread{
     87         
     88         Moneyclass a;
     89         
     90         getmoney(Moneyclass a){
     91             this.a=a;
     92         }
     93             
     94         public void run(){    //运行5次 取钱五次
     95             
     96             long outmoney=Math.round(Math.random()*100);
     97             
     98             for(int i=0;i<5;i++){
     99                 a.get((int) outmoney);            
    100             }    
    101             
    102         }
    103         
    104         
    105     }
    106 
    107 
    108     /**
    109      * @author lisj
    110      * 设置存钱线程
    111      */
    112     class putmoney extends Thread{
    113         
    114         Moneyclass b;
    115         
    116         putmoney(Moneyclass b){
    117             this.b=b;
    118         }
    119             
    120         public void run(){    //存钱五次
    121             
    122             for(int i=0;i<5;i++){
    123                 b.put();            
    124             }    
    125             
    126         }        
    127     }
  • 相关阅读:
    Leetcode 274.H指数
    Leetcode 273.整数转换英文表示
    Leetcode 264.丑数II
    Leetcode 260.只出现一次的数字III
    Leetcode 242.有效的字母异位词
    Leetcode 241.为运算表达式设计优先级
    Leetcode 240.搜索二维矩阵II
    Leetcode 239.滑动窗口最大值
    Leetcode 237.删除链表中的节点
    Leetcode 236.二叉树的最近公共祖先
  • 原文地址:https://www.cnblogs.com/lifescolor/p/3874090.html
Copyright © 2011-2022 走看看