Lets suppose, that next code works in application, which uses only one stream to call changeRoll

// ClassRoll.java
public class ClassRoll {
    private HashMap students = new HashMap();
    private void addStudent ( Student stud ) {
        students.put(stud.getName(), stud) ;
    }

    private void removeStudent( Student stud ) {
        students.remove( stud.getName() ) ;
    }

    public boolean changeRoll( int code, Student stud ) {
        switch( code ) {
            case 1: addStudent( stud );
                    return true ;
            case 2: removeStudent( stud );
                    return true ;
            default : return false ;
        }
    }

    public boolean isInRoll(Student stud) {
        return students.containsKey(stud.getName());
    }
}


// Student.java
public class Student {
    private String name;

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
U have to edit code so application could work safe with any number of streams. Choose an optimal set of changes, which leads to success.
Explanation
changeRoll and isInRoll are the only public methods with access to shared resource (hash table students). It is enough to make calls to them synchronized, to accomplish the task.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes