在一個程式中,這些獨立執行的程式片斷叫作“執行緒”(Thread),利用它程式設計的概念就叫作“多執行緒處理”。下面小編準備了關於Java多執行緒的實現方式,提供給大家參考!
Java多執行緒的實現方式
1. 繼承Thread類,實現run方法
2. 實現 Runnable介面,將該類繫結到新建的Thread物件上
class example Runnable
{
public void run()
{}
}
Invoke:
public static void main(String[] args)
{
Thread th = new Thread(new example());
t();
}
Java實現檔案下載並解決中文檔名亂碼
String filepath = "c:/";//需要下載的`檔案路徑
String filename = "文件";//需要下載的檔名字
//解決中文檔名亂碼問題
if (eader("User-Agent")werCase()xOf("firefox") > 0)
filename = new String(ytes("UTF-8"), "ISO8859-1");//firefox瀏覽器
else if (eader("User-Agent")perCase()xOf("MSIE") > 0)
filename = de(filename, "UTF-8");//IE瀏覽器
t();//如果有換行,對於文字檔案沒有什麼問題,但是對於其它格
//式,比如AutoCAD、Word、Excel等檔案下載下來的檔案中就會多出一些換行符//0x0d和0x0a,這樣可能導致某些格式的檔案無法開啟,有些也可以正常開啟。同//時t()這種方式也能清空緩衝區, 防止頁面中的空行等輸出到下載內容裡去
ontentType("application/octet-stream");
eader("Content-Disposition", "attachment;filename="" + filename + """);
eader("Connection", "close");
ServletOutputStream sos = utputStream();
FileInputStream fis = null;
File d = new File(filepath);
if (ts())
{
fis = new FileInputStream(filepath);//
byte b[] = new byte[1000];
int j;
while ((j = (b)) != -1)
{
try
{
e(b, 0, j);
}
catch (IOException exp)
{
}
}
e();
h();
e();
}