我有一个包含一堆方法的类。例如
private int forFunction(String exceptionFileList, FileInfo z, String compltetedFileList, String sourceDir)
{
atPDFNumber++;
exceptionFileList = "";
int blankImage = 1;
int pagesMissing = 0;
//delete the images currently in the folder
deleteCreatedImages();
//Get the amount of pages in the pdf
int numberPDFPage = numberOfPagesPDF(z.FullName);
//Convert the pdf to images on the users pc
convertToImage(z.FullName);
//Check the images for blank pages
blankImage = testPixels(@"C:\temp", z.FullName);
//Check if the conversion couldnt convert a page because of an error
pagesMissing = numberPDFPage - numberOfFiles;
}
现在我现在尝试的是在一个线程中访问该类..但不仅仅是一个线程,也许大约 5 个线程来加速处理,因为一个有点慢。
现在在我看来,那将是一片混乱……我的意思是一个线程更改变量,而另一个线程忙于处理它们等等,并在所有这些方法中锁定每个变量……不会有一个好的时间...
那么建议什么,不知道它是否正确..是这个
public void MyProc()
{
if (this method is open, 4 other threads must wait)
{
mymethod(var,var);
}
if (this method is open, 4 other threads must wait and done with first method)
{
mymethod2();
}
if (this method is open, 4 other threads must wait and done with first and second method)
{
mymethod3();
}
if (this method is open, 4 other threads must wait and done with first and second and third method)
{
mymethod4();
}
}
这是否是解决多个线程同时访问多个方法的问题的正确方法?
这些线程只会访问 Class 5 次,不会更多,因为工作负载将被平均分配。