2009-10-16 3 views
0

여러 스레드가 동시에 웹 서버의 파일에 대한 디렉토리를 폴링 할 수 있지만이 코드는 스레드 안전성이 있다고 간주됩니까?내 방법의 안전함

감사합니다, 마이크 Get a testimonial 코멘트에서 통화의 대부분

//Get a testimonial 
    virtualRoot = HostingEnvironment.ApplicationVirtualPath; 
    configuration = WebConfigurationManager.OpenWebConfiguration(virtualRoot); 
    pathToNewsDirectory = configuration.AppSettings.Settings["VanHinoWeb.news.dir"].Value; 
    fileListing = Directory.GetFiles(pathToNewsDirectory, "*xml"); 
    int index = generator.Next(0, fileListing.Length); 

    //Put it into XML and get data into string array to return 
    testimonialReader = new XmlTextReader(fileListing[index]); 
    testimonialReader.ReadToFollowing("quote"); 
    testimonialData[0] = testimonialReader.ReadString(); 
    testimonialReader.ReadToFollowing("author"); 
    testimonialData[1] = testimonialReader.ReadString(); 
    testimonialReader.ReadToFollowing("authorTitle"); 
    testimonialData[2] = testimonialReader.ReadString(); 

    return testimonialData; 
} 

답변

0

는 스레드에 안전해야한다. 호스팅 환경, 앱 설정 및 디렉토리 내용 나열에 액세스하는 것이 좋습니다.

그러나 generator 개체, testimonialReadertestimonialData 개체가 만들어지고 스레드간에 공유되는지 여부는 명확하지 않습니다. 스레드간에 공유되는 경우 코드는 스레드로부터 안전하지 않습니다.

+0

또한이 메서드에 사용 된 모든 개체가 메서드 내에서 선언되었음을 언급해야합니다. 그래서 나는 각 스레드가 자체적으로 객체 스택을 가질 것이라고 생각했습니다. –

+0

그럼 너 괜찮을거야. – LBushkin

0

파일 읽기 전용이므로 스레드로부터 안전합니다. 또한 XmlTextReader 사용을 마친 후에는 XmlTextReader (testimonialReader)를 닫아야합니다.