初学Java写的第一个多线程程序

类别:Java 点击:0 评论:0 推荐:

/**
 文件名 : FirstTread.java
 描述 :初学Java写的第一个多线程程序
 作者: 慈勤强   [email protected]
 参考:http://java.sun.com
  **/

import java.io.*;
import java.net.*;

public class FirstTread extends Thread
{
 
 public void run()
 {
  int i=0;
  try
  {
   String hostname = "www.flashempire.com";
   String strPage = "/theater/top10.php?id=3184";
   int port = 80;   
   InetAddress addr = InetAddress.getByName(hostname);
   for(int j=1;j<10;j++)
   {Socket socket = new Socket(addr, port); //建立一个Socket

   BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
   wr.write("GET " + strPage + " HTTP/1.0\r\n");
   wr.write("HOST:" + hostname + "\r\n");
   wr.write("\r\n");
   wr.flush();
   BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
   String line = rd.readLine();
   wr.close();
   rd.close();
   i++;
   System.out.println(i+"  "); 
   
   }
  }
  catch(Exception e)
  {
   System.out.println(e.toString());
  }
 } 
 public static void main(String args[])
 {    
  try
  {
    /**
    这里不知道如何写,就只能一个一个的写了,知道的麻烦告诉一声
    **/
   Thread t = new FirstTread();
   t.start();
   Thread t1 = new FirstTread();
   t1.start();
   Thread t2 = new FirstTread();
   t2.start();
   Thread t3 = new FirstTread();
   t3.start();
   Thread t4 = new FirstTread();
   t4.start();
   Thread t5 = new FirstTread();
   t5.start();
   Thread t6 = new FirstTread();
   t6.start();
   Thread t7 = new FirstTread();
   t7.start();
   Thread t8 = new FirstTread();
   t8.start();
   Thread t9 = new FirstTread();
   t9.start();
  }
  catch(Exception e)
  {
   System.out.println(e.toString());
  }
 }
}

本文地址:http://com.8s8s.com/it/it15121.htm