- 相關(guān)推薦
Java多線(xiàn)程的實(shí)現方式
在一個(gè)程序中,這些獨立運行的程序片斷叫作“線(xiàn)程”(Thread),利用它編程的概念就叫作“多線(xiàn)程處理”。下面小編準備了關(guān)于Java多線(xiàn)程的實(shí)現方式,提供給大家參考!
Java多線(xiàn)程的實(shí)現方式
1. 繼承Thread類(lèi),實(shí)現run方法
2. 實(shí)現 Runnable接口,將該類(lèi)綁定到新建的Thread對象上
class example Runnable
{
public void run()
{}
}
Invoke:
public static void main(String[] args)
{
Thread th = new Thread(new example());
th.start();
}
Java實(shí)現文件下載并解決中文文件名亂碼
String filepath = "c:/";//需要下載的文件路徑
String filename = "文檔.doc";//需要下載的文件名字
//解決中文文件名亂碼問(wèn)題
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0)
filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");//firefox瀏覽器
else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0)
filename = URLEncoder.encode(filename, "UTF-8");//IE瀏覽器
response.reset();//如果有換行,對于文本文件沒(méi)有什么問(wèn)題,但是對于其它格
//式,比如AutoCAD、Word、Excel等文件下載下來(lái)的文件中就會(huì )多出一些換行符//0x0d和0x0a,這樣可能導致某些格式的文件無(wú)法打開(kāi),有些也可以正常打開(kāi)。同//時(shí)response.reset()這種方式也能清空緩沖區, 防止頁(yè)面中的空行等輸出到下載內容里去
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
response.setHeader("Connection", "close");
ServletOutputStream sos = response.getOutputStream();
FileInputStream fis = null;
File d = new File(filepath);
if (d.exists())
{
fis = new FileInputStream(filepath);//
byte b[] = new byte[1000];
int j;
while ((j = fis.read(b)) != -1)
{
try
{
sos.write(b, 0, j);
}
catch (IOException exp)
{
}
}
fis.close();
sos.flush();
sos.close();
}
【Java多線(xiàn)程的實(shí)現方式】相關(guān)文章:
sun認證考試經(jīng)驗:多線(xiàn)程的幾種實(shí)現方法詳解01-22
Java如何實(shí)現簡(jiǎn)單的whois查詢(xún)03-16
Java語(yǔ)言的特點(diǎn)和實(shí)現機制02-27