首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

多线程的有关问题,请帮帮我

2011-11-08 
多线程的问题,请帮帮我importjava.io.BufferedInputStreamimportjava.io.BufferedReaderimportjava.io.F

多线程的问题,请帮帮我
import   java.io.BufferedInputStream;
import   java.io.BufferedReader;
import   java.io.File;
import   java.io.FileOutputStream;
import   java.io.FileReader;
import   java.io.IOException;
import   java.io.OutputStream;
import   java.net.URL;
import   java.net.URLConnection;
import   java.util.ArrayList;
public   class   Alexa   implements   Runnable   {

public   ArrayList   s   =   new   ArrayList();

public   static   void   main(String[]   args)   throws   IOException   {
Alexa   alexa   =   new   Alexa();
alexa.readFile( "c:\\sss.txt ");
Thread   t   =   new   Thread(alexa);
t.start();
}
public   void   run()   {

String   a   =   " ";
for   (int   i   =   0;   i   <   s.size();   i++)   {
a   =   (String)   s.get(i);
try   {
saveBinaryFile(a);
System.out.println( "i= "   +   i);
System.out.println( "a= "   +   a);
}   catch   (IOException   e)   {
//   TODO   Auto-generated   catch   block
e.printStackTrace();
}
}
}

public   void   saveBinaryFile(String   url1)   throws   IOException   {

BufferedInputStream   in   =   null;
OutputStream   out   =   null;
try   {

URL   url   =   new   URL(url1);
URLConnection   conn   =   url.openConnection();

in   =   new   BufferedInputStream(conn.getInputStream());

out   =   new   FileOutputStream(new   File( "c://tmp// "
+   System.currentTimeMillis()));
int   n   =   0;
byte[]   bytes   =   new   byte[1024];

while   ((n   =   in.read(bytes))   !=   -1)   {
out.write(bytes,   0,   n);
}
}   catch   (Exception   e)   {
}   finally   {
try   {
in.close();
out.close();
}   catch   (Exception   ioe)   {
ioe.getStackTrace();
}
}

}

public   void   readFile(String   path)   {
String   fileline;
ArrayList   domains   =   new   ArrayList();
try   {
BufferedReader   d   =   new   BufferedReader(new   FileReader(path));

while   ((fileline   =   d.readLine())   !=   null)   {
domains.add(fileline);
System.out.println(fileline);
}

}   catch   (Exception   e)   {
System.out.println( "readFile.Error:   "   +   e.getMessage()   +   "   "
+   e.getClass());
e.printStackTrace();
}   finally   {
try   {

}   catch   (Exception   e)   {

}
}
s   =   domains;
}

}
c:\\sss.txt   放了一些htm的连接,我想将这些页面下载下来,想用多线程,但是其中saveBinaryFile(a);不执行,但是
System.out.println( "i= "   +   i);
System.out.println( "a= "   +   a);却打印出来了,请问怎么回事?请指教,谢谢


[解决办法]
有什么错误信息啊?
[解决办法]
我试了你的代码

可以下载页面源代码

不知道你的不执行是什么状况


[解决办法]
你没把异常打出来看看吧:
c:\tmp\1175136790484 (系统找不到指定的路径。)

你的目录不存在!
加上下面代码:

File dir = new File( "c://tmp// ");
if (!dir.exists()){
dir.mkdirs();
}

out = new FileOutputStream(new File( "c://tmp// "+ System.currentTimeMillis()));
之前
[解决办法]
} catch (Exception e) {
} finally {}
这样只捕获异常不处理异常,你看不到异常状态的
[解决办法]
700多线程同时跑。。。。
不妨你试试线程池管理,每次同时跑10-20个先程就可以了:
ThreadPool thePool = new DefaultThreadPool(10);
thePool.invokeLater(
new Thread() {
public void run() {
.....
}
}

还有一种办法:
MultiThreadedHttpConnectionManager manager=new MultiThreadedHttpConnectionManager();
manager.getParams().setConnectionTimeout(20000);
manager.getParams().setSoTimeout(10000);
manager.getParams().setDefaultMaxConnectionsPerHost(20);
manager.getParams().setMaxTotalConnections(50);
HttpClient client = new HttpClient(manager);
String url= "...... ";
PostMethod postMethod = new PostMethod(url);
int statusCode = client.executeMethod(postMethod);
InputStream is = postMethod.getResponseBodyAsStream();
InputStreamReader isr = new InputStreamReader(is, "gb2312 ");
BufferedReader in = new BufferedReader(isr);
String inputLine = in.readLine();
StringBuffer resultBuffer = new StringBuffer();
while (inputLine != null) {
resultBuffer.append(inputLine);
resultBuffer.append( "\n ");

inputLine = in.readLine();
}

String result = resultBuffer.toString();
......


热点排行