简体   繁体   中英

does Thread priority affect the synchronized block access?

I have a single FileScanner Thread that adds new files in a list and multiple FileParser Threads that get new files and each parses its own files. for synchronization sake, I placed the addition to the list and reading from the list in synchronized resource block. the problem is that sometimes the FileScanner Thread seems to starve and doesn't enter the synchronized block waiting for other FileParser threads to release the resource (the list). my question is, if I set the maximum priority to my FileScanner and minimum one to other FileParser threads, would it resolve the problem? in other words, does thread priority has any impact in order for JVM to choose among threads to grant access to synchronized block? Thanks.

UPDATE:

private List<ScannerFile> scannedFiles = Collections.synchronizedList(new LinkedList<ScannerFile>()) ;

this is called in my FileScanner thread:

    synchronized(scannedFiles){
        for(ScannerFile f: newList)
            try{
                scannedFiles.add(f);
            }
            catch(ConcurrentModificationException e){
                logger.error(e);
            }
    }

And this is called in my FileParser threads:

synchronized(scannedFiles){
    try{
        for(ScannerFile f: scannedFiles){
            if(parserName.equals(f.getParserName()) && f.isNew() == true){
                listNewFiles.add(f);
            }           
        }
        return listNewFiles;
    }
    catch(ConcurrentModificationException e){
        logger.trace(e);
        return new ArrayList<ScannerFile>();
    }
}

Thread priority is a hint or advice, that may or may not have an impact. Therefore it is dangerous to rely on priority settings for correctness of a concurrent program.

From "Java Concurrency In Practice" (Goetz et al):

Avoid the temptation to use thread priorities, since they can increase platform dependence and can cause liveness problems.[...]

If your program encounters deadlocks, there is a problem with the concurrency logic. Changing thread priorities would (in unlikely the best case) mask that problem or (in the likely case) introduce undetermined, hardware specific behavior.

The description of your problem, looks like a prime example for a ReadWriteLock . But without a concise example of your code, it is hard to give a sound advice.

Thread priority should not make or break synchronization. Sounds like a problem in your blocking collection, which you should not have to implement yourself anyway. Take a look at LinkendBlockingQueue and friends in java.util.concurrent.

There is a logic error. This should work (i did not have an IDE to reconfirm):

LinkedBlockingQueue<ScannerFile> newList = new LinkedBlockingQueue<>();
LinkedBlockingQueue<ScannerFile> scannedFiles = new LinkedBlockingQueue<>();
for(;;){

try{
ScannerFile f = newList.get();
scannedFiles.add(f);
}
catch(InterruptedException ex{
Thread.currentThread.interrupt();
//log
break;
}
}

ThreadParser will go like this:

for(;;){
try{
ScannedFile f= scannedFiles.get();
//other logic goes here
}catch(InterruptedException ex{
Thread.currentThread.interrupt();
//log
break;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM