【问题标题】:run 5 thread in parallel [duplicate]并行运行5个线程[重复]
【发布时间】:2015-07-14 03:15:29
【问题描述】:

https://drive.google.com/folderview?id=0B4cxDnPAjctLfmwxSlZhcFhDZEd4aF9QQnBaZWpQT1VhSnN4UE4xdG9NcDc1Nm55YzZzdW8&usp=sharing 上面这个链接中的代码 main方法运行5个线程 当我运行代码时,每个线程都与其他线程共享变量 并且输出应该在这个结构中 E:\TIPZIL8\1\osama\2\osama12.pdf E:\TIPZIL8\2\ali\3\Ahmed23.htm E:\TIPZIL8\3\ali\4\amr34.htm E:\TIPZIL8\4\ali\5\hossam45.htm E:\TIPZIL8\5\al​​i\6\ali56.htm

但输出不同

  public class GetPDFLink implements Runnable {
private String id;
private String link;
private String info;
private String name;

public GetPDFLink(String id,String link,String info,String name) {
    this.id = id;
    this.link = link;
    this.info = info;
    this.name = name;
}
public void run() {
    Download a = new Download();
    try {
        System.out.println(name + " is running");
        System.out.println(id+"&&&&&&&&&&&");
        a.downloadHTMLWithPDFolder(id, link, info, name);
    } catch (IOException ex) {
        Logger.getLogger(GetPDFLink.class.getName()).log(Level.SEVERE, null,           ex);
    }
    try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {
        Logger.getLogger(GetPDFLink.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("after sleeping");
}

public static void main(String[] args) throws InterruptedException {
    GetPDFLink m_1 = new GetPDFLink("1","http://ir.pdf.com/secfiling.cfm?filingID=1437749-15-7272","pdf1","osama");
    GetPDFLink m_2 = new GetPDFLink("2","http://ir.pdf.com/secfiling.cfm?filingID=1437749-15-3861","pdf2","Ahmed");
    GetPDFLink m_3 = new GetPDFLink("3","http://ir.pdf.com/secfiling.cfm?filingID=1437749-15-3698","pdf3","amr");
    GetPDFLink m_4 = new GetPDFLink("4","http://ir.pdf.com/secfiling.cfm?filingID=1437749-15-3369","pdf4","hossam");
    GetPDFLink m_5 = new GetPDFLink("5","http://ir.pdf.com/secfiling.cfm?filingID=1437749-15-3369","pdf5","ali");
    Thread t1 = new Thread(m_1);
    Thread t2 = new Thread(m_2);
    Thread t3 = new Thread(m_3);
    Thread t4 = new Thread(m_4);
    Thread t5 = new Thread(m_5);
    t1.start();
    t2.start();
    t3.start();
    t4.start();
    t5.start();
    System.out.println("this might print first!");

}

}

    public class Download{        
    static String company_id = "";
    static String company_Name = "";
    static String pdfInfo = "";
    static String pdfLinkDownload = "";
    static String s= "";
        public void downloadHTMLWithPDFolder(String company_id,String pdfLinkDownload,String pdfInfo,
        String company_Name)
        throws MalformedURLException, IOException {
            int company = 0;
    Download.company_id = company_id;
    Download.company_Name = company_Name;
    Download.pdfInfo = pdfInfo;
    Download.pdfLinkDownload = pdfLinkDownload;
    BufferedInputStream in = null;
    FileOutputStream fout = null;
    File file=new File("E:\\ALL");

    company = Integer.parseInt(company_id);
    company ++;

    s =  ""+company;

    if(!file.exists())
    {
        file.mkdirs();
    }

    File fileid=new File("E:\\ALL\\"+company_id);

    if(!fileid.exists())
    {
        fileid.mkdirs();
    }   

    File file1=new File("E:\\ALL\\"+company_id+"\\"+company_Name);

    if(!file1.exists())
    {
        file1.mkdirs(); 
    }

    File filec=new File("E:\\ALL\\"+company_id+"\\"+company_Name+"\\"+s);

    if(!filec.exists())
    {
        filec.mkdirs(); 
    }      

    try {
        System.out.println("open...");
        pdfInfo.replaceAll("/","");
        fout  =new FileOutputStream("E:\\ALL\\"+company_id+"\\"+company_Name+"\\"+s+"\\"+company_Name+company_id+company+".htm");
        System.out.println("LINK : " + pdfLinkDownload);
        in = new BufferedInputStream(new URL(pdfLinkDownload).openStream());


        final byte data[] = new byte[1024];
        int count;
        while ((count = in.read(data, 0, 1024)) != -1) {
            fout.write(data, 0, count);
        }
    } 
    finally
    {
        if (in != null)
        {
            in.close();
        }
        if (fout != null) {
            fout.close();
        }
        System.out.println("fileDwonloaded   ....***");
    }
}

}

编辑 :: 通过删除静态变量解决的问题

【问题讨论】:

    标签: java multithreading


    【解决方案1】:

    Download 类中的字段为static。删除每个字段的 static 修饰符。您还应该更新类中这些字段的用法。

    这个类看起来像这样:

    public class Download {
        String company_id = "";
        String company_Name = "";
        String pdfInfo = "";
        String pdfLinkDownload = "";
        String s= "";
    
        public void downloadHTMLWithPDFolder(String company_id,
            String pdfLinkDownload,
            String pdfInfo,
            String company_Name)
            throws MalformedURLException, IOException {
                int company = 0;
            this.company_id = company_id;
            this.company_Name = company_Name;
            this.pdfInfo = pdfInfo;
            this.pdfLinkDownload = pdfLinkDownload;
            BufferedInputStream in = null;
            FileOutputStream fout = null;
            File file=new File("E:\\ALL");
    
            company = Integer.parseInt(company_id);
            company ++;
            s =  ""+company;
    
            if(!file.exists()) {
                file.mkdirs();
            }
    
            File fileid=new File("E:\\ALL\\"+company_id);
            if(!fileid.exists()) {
                fileid.mkdirs();
            }
            File file1=new File("E:\\ALL\\"+company_id+"\\"+company_Name);
            if(!file1.exists()) {
                file1.mkdirs();
            }
            File filec=new File("E:\\ALL\\"+company_id+"\\"+company_Name+"\\"+s);
            if(!filec.exists()) {
                filec.mkdirs();
            }
            try {
                System.out.println("open...");
                pdfInfo.replaceAll("/","");
                fout  =new FileOutputStream("E:\\ALL\\"+company_id+"\\"+company_Name+"\\"+s+"\\"+company_Name+company_id+company+".htm");
                System.out.println("LINK : " + pdfLinkDownload);
                in = new BufferedInputStream(new URL(pdfLinkDownload).openStream());
    
    
                final byte data[] = new byte[1024];
                int count;
                while ((count = in.read(data, 0, 1024)) != -1) {
                    fout.write(data, 0, count);
                }
            }
            finally
            {
                if (in != null)
                {
                    in.close();
                }
                if (fout != null) {
                    fout.close();
                }
                System.out.println("fileDwonloaded   ....***");
            }
        }
    }
    

    【讨论】:

    • 现在可以使用了,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多