java吧 关注:1,291,173贴子:12,820,214
  • 2回复贴,共1

请教大神,Java多线程应用

只看楼主收藏回复

一个学校有多名老师,学校负责向老师的工资卡上发工资,教师从各自的账户上领取工资
拜托拜托


1楼2015-01-08 16:17回复
    我这里只有一个只实现了单线程的,不知道如何实现多线程啊


    2楼2015-01-08 16:18
    回复
      2025-12-14 01:50:13
      广告
      不感兴趣
      开通SVIP免广告
      class TeacherAccount{
      private int[] momth= new int[13];
      private int num=0;
      public synchronized void deposit(int mon){
      num++;
      nonth[num]=mon;
      this.notify();
      }
      public synchronized int withdraw(){
      while(num==0){
      try{ this.wait();}
      catch(InterruptedException e){}
      }
      nun--;
      return month[num+1];
      }
      }
      class SchoolBank implements Runnable{
      TeacherAccount account;
      public SchoolBank(TeacherAccount s){account=s;}
      public void run(){
      for(int i=1;i<7;i++){
      account.deposit(i);
      System.out.println("学校发放:"+i+"月份工资");
      try{
      Thread.sleep((int)Math.random()*10);
      }
      catch(InterruptedException e){}
      }
      }
      }
      class Teacher implements Runnable{
      TeacherAccount account;
      public Teacher(TeacherAccount s){account=s;}
      public void run(){
      int temp;
      for(i=1;i<12;i++){
      temp=account.withdraw();
      System.out.println("教师提取:"+temp+"月份工资");
      try{
      Thread.sleep((int)Math.random()*10);
      }
      catch(InterruptedException e){}
      }
      }
      }
      public class TeacherAccountTest{
      public static void main(String[] args){
      TeacherAccount staffaccount=new TeacherAccount();
      SchoolBank con=new SchoolBank(staffaccount);
      Teacher sta=new Teacher(staffaccount);
      Thread t1=new Thread(com);
      Thread t2=new Thread(sta);
      t1.start();
      t2.start();
      }
      }


      3楼2015-01-08 16:43
      回复