public class Group {
    ArrayList contents;
    public Group() {
        lst = new java.util.ArrayList();
    };
    public void add(String s) {
        /* code to add something to contents here */
        };
    public void reverse() {
        /* code to reverse the contents here */
    };
}
public class GroupUser extends Thread {
    Group group;
    public GroupUser(Group g) {
        group = g;
    }
    public void changeGroup() {
        /* code that calls group.add() several times then group.reverse() */
    }
    public void run() {
        for (int i = 0; i < 10; i++) changeGroup();
    }
}
public static void main(String args[]) throws InterruptedException {
    Group g = new Group();
    new GroupUser(g).start();
    new GroupUser(g).start();
    new GroupUser(g).start();
}
Which of the following changes could you make to the code to prevent the three threads started by main from interfering with each other’s use of the shared Group instance (provided by main) in GroupUser.changeGroup?   
Question Number 17 add the keyword synchronized before void in GroupUser.changeGroup add the keyword synchronized before void in both Group.add and Group.reverse
--------------
public class Group {
    ArrayList contents;
    public Group() {
        lst = new java.util.ArrayList();
    };
    public void add(String s) {
        /* code to add something to contents here */
        };
    public void reverse() {
        /* code to reverse the contents here */
    };
}
 
public class GroupUser extends Thread {
    Group group;
    public GroupUser(Group g) {
        group = g;
    }
    public void changeGroup() {
        /* code that calls group.add() several times then group.reverse() */
    }
    public void run() {
        for (int i = 0; i < 10; i++) changeGroup();
    }
}
 
public static void main(String args[]) throws InterruptedException {
    Group g = new Group();
    new GroupUser(g).start();
    new GroupUser(g).start();
    new GroupUser(g).start();
}
Which of the following changes could you make to the code to prevent the three threads started by main from interfering with each other’s use of the shared Group instance (provided by main) in GroupUser.changeGroup?   
Question Number 18
change the call to changeGroup in GroupUser.run to:synchronized(group) {
    changeGroup();
}
change the call to changeGroup in GroupUser.run to:synchronized(this) {
    changeGroup();
----------
Iterator list= new iterator()
list = vect1.iterator()
while(list.hasNext())
{
    String element = (String)list.next();
    System.out.print(element);
}
 
Iterator list= new iterator()
list = vect1.iterator()
while(vect1.hasNext())
{
    String element = (String)vect1.next();
    System.out.print(element);
}
 
Iterator list= vect1.iterator()
while(list. vect1.hasNext())
{
    String element = (String)list. vect1.next();
    System.out.print(element);

解决方案 »

  1.   

    add the keyword synchronized before void in both Group.add and Group.reverse
      

  2.   

    17 add the keyword synchronized before void in both Group.add and Group.reverse
    18 change the call to changeGroup in GroupUser.run to:synchronized(group) {
      changeGroup();
    }
      

  3.   

    Which of the following changes could you make to the code to prevent the three threads started by main from interfering with each other's use of the shared Group instance (provided by main) in GroupUser.changeGroup?看这个问题,既然是为了防止 各个线程互相妨碍对共享的Group实例的使用的话,就应该对共用的Group实例同步
      

  4.   


    change the call to changeGroup in GroupUser.run to:synchronized(group) {
      changeGroup();
    }同步group对象就可以了,若同步this只是同步调用的当前的线程.
    由于main中的同步只要其互相不干涉,故同步修改数据的changGroup()就可,若还有其它会访问add.和reverse
    则需在相应的方法上加上synchronized