同志们,能举些多线程的例子给哥看看吗?

解决方案 »

  1.   

    http://topic.csdn.net/u/20100627/18/dfd80ebc-63a1-4132-ab07-25a45e1ef620.html?88886
      

  2.   

    生产者消费者===现成同步举例;package lession.lession6;
    import java.util.*;
    //==========================模拟销售馒头的线程同步============================
    /*
    class Ham{
     public static Object box=new Object();
     public static   int material=6;  //有材料6个可以做6个 馒头
     public static   int sales=0;
     public static   int production=3;
    }
    class Hammaker extends Thread{
     private void Make(){
      synchronized(Ham.box){
       Ham.production++;
       int num=Ham.production-Ham.sales;
       System.out.println("<厨师>:这里有馒头"+num+"个,拿去卖");
       Ham.box.notify();   // 唤醒服务员线程 如果把这里注释的话,会让服务员线程一直等待了,
      }
     }
     public void run(){
      while(Ham.production<=Ham.material){
       try{
        sleep(3000);
       }catch(InterruptedException ie){}
       Make();    //没隔1秒做一个馒头
      }
     }
    }
    class HAssistant extends Thread{
     private void Sell(){
      synchronized(Ham.box){
       if(Ham.production==Ham.sales){
        System.out.println("<服务员>:请稍后,馒头马上就来");
        try{
         Ham.box.wait();
        }catch(InterruptedException ie){}
       }   
       int num=Ham.production-Ham.sales;
       Ham.sales++;
       System.out.println("<服务员>:顾客门,馒头来了哦,一共有"+num+"个");    
       
      }
     }
     public void run(){
      while(Ham.sales<=Ham.material){
       System.out.println("=============<顾客门来买馒头了>===========");
       Sell();
       try{
        sleep(1000);//每隔一秒就卖一个馒头
       }catch(InterruptedException ie){}
      }
     }
    }
    public class thread1{
     public static void main(String []args){
      Hammaker maker=new Hammaker();
      HAssistant assistant =new HAssistant();
      maker.start();
      assistant.start();
      try{
       maker.join();           //等待该线执行完毕
       assistant.join();
      }
      catch(InterruptedException ie){}
      System.out.println();
      System.out.println(Ham.sales+"\n"+Ham.production);
     }
    }
    /*
      

  3.   

    http://download.csdn.net/source/741733 也可以下载个。