java - Can't understand barrier code -
i ran across barrier code , cannot understand barrierpost
method.
im supposed use code solve exercise 2 teams of threads race eachother count 10000.
i don't understand why same condition has different results opposite
public class barrier { private int currentposters = 0, totalposters = 0; private int passedwaiters = 0, totalwaiters = 1; /** * @param totalposters - nr of threads required start waiting threads * @param totalwaiters - nr of threads started later */ public barrier (int totalposters, int totalwaiters) { this.totalposters = totalposters; this.totalwaiters = totalwaiters; } public synchronized void init(int i) { totalposters = i; currentposters=0; } public synchronized void barrierset(int i) { totalposters = i; currentposters=0; } public synchronized void barrierwait() { boolean interrupted = false; while (currentposters = totalposters) { try {wait();} catch (interruptedexception ie) {interrupted=true;} } passedwaiters++; if (passedwaiters == totalwaiters) { currentposters = 0; passedwaiters = 0; notifyall(); } if (interrupted) thread.currentthread().interrupt(); } public synchronized void barrierpost() { boolean interrupted = false; // in case poster thread beats barrierwait, keep count of posters. while (currentposters == totalposters) { try {wait();} catch (interruptedexception ie) {interrupted=true;} } currentposters++; if (currentposters == totalposters) notifyall(); if (interrupted) thread.currentthread().interrupt(); } }
can help?
Comments
Post a Comment