This question already has an answer here:
这个问题在这里已有答案:
- Are static variables shared between threads? 7 answers
线程之间是否共享静态变量? 7个答案
https://drive.google.com/folderview?id=0B4cxDnPAjctLfmwxSlZhcFhDZEd4aF9QQnBaZWpQT1VhSnN4UE4xdG9NcDc1Nm55YzZzdW8&usp=sharing code in this link above the main method run 5 threads when I run the code every thread share variables with other thread and output should in this structure 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\ali\6\ali56.htm
https://drive.google.com/folderview?id=0B4cxDnPAjctLfmwxSlZhcFhDZEd4aF9QQnBaZWhQT1VhSnN4UE4xdG9NcDc1Nm55YzZzdW8&usp=sharing代码在此链接上面的主方法运行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 \ ali \ 6 \ ali56.htm
but the out put different
但是出局不同
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 ....***");
}
}
}
Edit :: The Problem Solved by remove static variables
编辑::解决问题的方法是删除静态变量
1 个解决方案
#1
Fields in Download
class are static
. Remove the static
modifier for each field. You should also update the usage of these fields in the class.
Download类中的字段是静态的。删除每个字段的static修饰符。您还应该更新类中这些字段的用法。
The class would look like this:
这个类看起来像这样:
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 ....***");
}
}
}
#1
Fields in Download
class are static
. Remove the static
modifier for each field. You should also update the usage of these fields in the class.
Download类中的字段是静态的。删除每个字段的static修饰符。您还应该更新类中这些字段的用法。
The class would look like this:
这个类看起来像这样:
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 ....***");
}
}
}