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);
}
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);
}
解决方案 »
- 求教SSH登录
- 上海找工作的黄金期是什么时候
- ssh整合tomcat启动报错UnabletolocateSpringNamespaceHandlerforXMLschemanamespace
- 求教:--jsp+struts1.2---action 中向前台jsp页面 返回值
- 如何在javascript中,把某个子符串赋给java中的字符串变量呢?
- 正则表达式
- 万分火急请教各位高手,在线等待
- 怎么才能得到一个文件的创建日期,修改日期等信息?
- 有关 jdom的问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 怎么用exe程序访问web服务器
- hibernate 查询语句报错
- 求帮助,走过的路过的都来看看!
18 change the call to changeGroup in GroupUser.run to:synchronized(group) {
changeGroup();
}
change the call to changeGroup in GroupUser.run to:synchronized(group) {
changeGroup();
}同步group对象就可以了,若同步this只是同步调用的当前的线程.
由于main中的同步只要其互相不干涉,故同步修改数据的changGroup()就可,若还有其它会访问add.和reverse
则需在相应的方法上加上synchronized