sun网站上就有例子。多线程的例子,你可以去下啊。省300

解决方案 »

  1.   

    import java.lang.Runnable;
    import java.lang.Thread;/*****************************************************
     *
     *本程序实现两个线程,每个线程输出1到100的数字
     *第一个线程输出1-10,停止,通知第二个线程 输出1-10 第二个线程停止 通知第一个线程 输出11-20 ...
     *
     *****************************************************/
    //通过Runnable接口创建的线程
    public class DemoThread implements Runnable{  public DemoThread() {
               //声明两个对象实例,即创建两个线程
             TestThread testthread1 = new TestThread(this,"1");
             TestThread testthread2 = new TestThread(this,"2");         //调用strat()方法启动线程
             testthread2.start();
             testthread1.start();
      }  //声明主方法
      public static void main(String[] args) {    DemoThread demoThread1 = new DemoThread();  }   //自定义线程的run()方法
       public void run(){        TestThread t = (TestThread) Thread.currentThread();
            try{
              if (!t.getName().equalsIgnoreCase("1")) {
                  synchronized(this) {
                   //调用wait()方法,释放对象锁标志,进入等待状态
                      wait();
                  }
              }
              while(true){            System.out.println("@time in thread"+ t.getName()+ "="+ t.increaseTime());            if(t.getTime()%10 == 0) {
                  synchronized(this) {
                    System.out.println("****************************************");
                    //调用notify()方法通知等待队列中的第一个线程
                    notify();
                    //输出到达100后跳出循环进入等待状态
                    if ( t.getTime()==100 ) break;
                    wait();
                }
              }
            }
            }catch(Exception e){e.printStackTrace();}
        }}//通过Thread类的子类创建的线程
    class TestThread extends Thread{
        private int time = 0 ;
        public TestThread(Runnable r,String name){
          super(r,name);
        }
        //获取输出的数字
        public int getTime(){
           return time;
        }
        //增加数字的输出次数
        public int increaseTime (){
           return ++time;
        }
    }
      

  2.   

    我给你一些文章你读一下,不知是不是你想要的
    http://www.yesky.com/20020919/1631257.shtml Java多线程程序设计入门
    http://cn.tech.yahoo.com/021021/31/1abg0.html 用Java实现多线程服务器程序
      

  3.   


    http://www-900.ibm.com/developerWorks/cn/java/l-jsvr/index.shtml 使用JAVA建立稳定的多线程服务器 
      

  4.   

    感谢josy(风尘浪子) 的程序