`

jdk6.0从入门到精通-----chapter8并发多线程(2)

阅读更多
在1的条件下,改为多客户端响应以及可以维护线程的状态,在需要时改为执行,不需要时等待
客户端代码见jdk6.0从入门到精通-----chapter8并发多线程(1)所示

package ThreadPool1;

import java.io.*;
import java.lang.*;
import java.net.*;
import java.text.*;

class ThreadManager
{	
  TheThread[] runner=null;
  int count;
	
  public static void main(String[] args) throws IOException
  {	
    new ThreadManager(5).listen();
  }
  
  public ThreadManager(int count)
  {
    this.count=count;	
  }	
  
  private void listen() throws IOException
  {
    //建立线程池,启动所有线程
    runner=new TheThread[this.count];
    for(int i=0;i<this.count;i++)
    {
      runner[i]=new TheThread(i);
      runner[i].start();	
    }		
    //侦听1024端口
    ServerSocket serverSocket = new ServerSocket(1024);	
    while(true)
    {
      final Socket socket = serverSocket.accept();
      Thread worker=new Thread()
      {
      	public void run()
      	{
          while(true)
          {
            try
            {
              BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              String cmd=in.readLine();
              if(cmd==null)
                break;
              System.out.println("收到命令:"+cmd);
              if(cmd.startsWith("run")) 
                for(int i=0;i<5;i++)
                { 	
                  synchronized(runner[i]) 
                  {  
                    runner[i].notify(); 
                    runner[i].command=0;
                    runner[i].status=1; 
                  }  
                }
              if(cmd.startsWith("pause"))
                for(int i=0;i<5;i++) 
                {
                  synchronized(runner[i])
                  {
                    runner[i].command=1;
                  }
                }
              if(cmd.startsWith("step"))
                for(int i=0;i<5;i++)
                {
                  synchronized(runner[i])
                  {
                    if(runner[i].status==0)
                    {	
              	      runner[i].notify(); 
                      runner[i].command=0;
              	      runner[i].status=1;
              	      break;
                    }  
                  }  	 	
                }
              if(cmd.startsWith("report"))
              {
                System.out.print("线程池中共有5个线程,");
                int workingThreadCount=0;
                for(int i=0;i<5;i++)
                  if(runner[i].status==1)
                    workingThreadCount++;
                System.out.println("其中"+workingThreadCount+"个线程处于工作状态");    	
              }
              if(cmd.startsWith("stop"))
              {
                for(int i=0;i<5;i++)
                {
                  synchronized(runner[i])
                  {
                    Thread.sleep(500);
                    runner[i].interrupt(); 	  
                  }  	 	
                }
                System.out.println("线程池已被销毁,程序退出");
                break;
              }  		  	  
            }
            catch(Exception e)
            {
              e.printStackTrace();	
            }
          }
        }
      };
      worker.start();	    
    }  	    	
  }		  	
}	

class TheThread extends Thread
{
  int no;
  int command=0;
  int status;//0:空闲中;1:运行中
  
  public TheThread(int i)
  {
    this.no=i;	
  }
  	
  public void run()
  {
    try	
    {
      this.status=1;
      while(true)
      { 
        Thread.sleep(1000);
        if(this.command==1) 
          synchronized(this)
          {
            System.out.println("["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date())+"] "+"线程"+this.no+"开始等待...");
            this.status=0;
            this.wait();
          } 
      }
    }  
    catch(InterruptedException ie)
    {
      System.out.println("["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date())+"] "+"线程"+this.no+"被中止...");
    }	     
  }  	    	
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics