日期:2014-05-20  浏览次数:20706 次

大家帮我看下这段程序为什么会出现死锁,谢谢
import java.util.Hashtable;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Logger;

public class Download implements Runnable {

static public Queue<DownloadPage> taskQueue = new ConcurrentLinkedQueue<DownloadPage>();
static public Map<String, CacheStorage> dict = new Hashtable<String, CacheStorage>();

public static void main(String[] args) {
Download d1 = new Download();
Download d2 = new Download();
Download d3 = new Download();
new Thread(d1).start();
new Thread(d2).start();
new Thread(d2).start();

}

static Boolean DoTask(DownloadPage dp) throws Exception {
System.out.println(Thread.currentThread().getName()+"进入进程");
if (dp == null) {
return true;
}
String DownloadHtmlUrl = dp.RemoteUrl;
CacheStorage cs = null;
cs = dict.get(DownloadHtmlUrl);
if (cs == null) {
CacheStorage ct = null;
ct = new CacheStorage();
ct.Local = "";
ct.status = "";
ct.Url = DownloadHtmlUrl;
dict.put(DownloadHtmlUrl, ct);
System.out.println("ERR1");
return false;
}
if (cs.status.equals("done")) {
return true;
}
if (cs.status == "downloading") {
return false;
}
if (cs.status == "pending") {
return false;
}
for (String u : dp.DependencyUrls) {

CacheStorage csDependency = dict.get(u);
if (csDependency == null) {

csDependency = new CacheStorage();
csDependency.Local = "";
csDependency.status = "";
csDependency.Url = u;
dict.put(u, csDependency);
if (csDependency.status != "downloadng") {
csDependency.status = "downloading";
Thread.sleep(2000);
System.out.println("Downloading" + csDependency.Url);
csDependency.status = "done";
csDependency.Local = "Download local file folder";

return false;
}

return false;
}
if (csDependency.status != "done") {

cs.status = "pending";
DownloadPage down = new DownloadPage();
down.RemoteUrl = csDependency.Url;
taskQueue.offer(down);
return false;
}
}
cs.status = "downloading";
System.out.println("Downloading" + cs.Url);
Thread.sleep(2000);
cs.status = "done";
cs.Local = "Download local file folder";
// System.out.println(cs.Url+"下载完成");
System.out.println(Thread.currentThread().getName()+"离开进程");
return true;

}

@Override
public void run() {
// TODO Auto-generated method stub
DownloadPage downloadPage = new DownloadPage();
downloadPage.RemoteUrl = "a.html";
downloadPage.DependencyUrls = new String[] { "1,jpg", "2.jpg" };
taskQueue.offer(downloadPage);
downloadPage = new DownloadPage();
downloadPage.RemoteUrl = "b.html";
downloadPage.DependencyUrls = new String[] { "21,jpg", "22.jpg",
"44.jpg" };
taskQueue.offer(downloadPage);
downloadPage = new DownloadPage();
downloadPage.RemoteUrl = "c.html";
downloadPage.DependencyUrls = new String[] { "31,jpg", "32.jpg" };
taskQueue.offer(downloadPage);
downloadPage = new DownloadPage();
downloadPage.RemoteUrl = "d.html";
downloadPage.DependencyUrls = new String[] { "41,jpg", "42.jpg" };
taskQueue.offer(downloadPage);
downloadPage = new DownloadPage();