用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心Java

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

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 18:53:39
/**
文件名 : FirstTread.java
描述 :初学Java写的第一个多线程程序
作者: 慈勤强 cqq1978@yeah.net
参考: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());
}
}
}


Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:281.25000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号