Which of the four are valid modifications to synchronize access to the valid list between threads t1 and t2?

Given:

Which of the four are valid modifications to synchronize access to the valid list between threads t1 and t2?

Given:

Which of the four are valid modifications to synchronize access to the valid list between threads t1 and t2?

A.
Replace line 1 with:
Synchronized (t2) (t1.start();) synchronized(t1) (t2.start(); )
korrekte Schreibweise: synchronized (t2) {t1.start();} synchronized(t1) { t2.start();}

B.
Replace Line 2 with:
static CopyWriteArrayList<Integer> list = new CopyWriteArrayList<>();
korrekte Schreibweise: static CopyOnWriteArrayList<Integer> list = new
CopyOnWriteArrayList<>();

C.
Replace line 3 with:
synchronized public static void addItem () {
korrekte Schreibweise: synchronized public static void addItem () {

D.
Replace line 4 with:
synchronized (list) (list.add(1);)
korrekte Schreibweise: synchronized (list) { (list.add(1); }

E.
Replace line 5 with:
Synchronized public void run () {
korrekte Schreibweise: synchronized public void run () {

F.
replace line 6 with:
Synchronized (this) {for (in i = 0, i<5000, i++) WorkPool.addItem(); }
korrekte Schreibweise: synchronized (this) {for (int i = 0; i<500; i++) WorkPool.addItem(); }

G.
Replace line 6 with:
synchronized (bar) {for (int i= 0; i<5000; i++) WorkPool.addItem(); }
korrekte Schreibweise: synchronized (bar) {for (int i= 0; i<500; i++) WorkPool.addItem(); }

Explanation:

Away to create synchronized code is with synchronized statements.
Unlike synchronized methods, synchronized statements must specify the object that provides
theintrinsic lock:
For example:
public void addName(String name) {
synchronized(this) {
lastName = name;
nameCount++;
}
nameList.add(name);
}
In this example, the addName method needs to synchronize changes to lastName and
nameCount, but alsoneeds to avoid synchronizing invocations of other objects’ methods. Without
synchronized statements, therewould have to be a separate, unsynchronized method for the sole
purpose of invoking nameList.add.
Reference: The Java Tutorial,Intrinsic Locks and Synchronization



Leave a Reply 7

Your email address will not be published. Required fields are marked *


Tim

Tim

The answer is B C D G.

I tested G, and it can effectively prevent ArrayIndexOutOfBoundsException (which is caused by concurrent modification of two threads).

Charles

Charles

Should be B C D F

Humberto Bañuelos Flores

Humberto Bañuelos Flores

Answer: F
Away to create synchronized code is with synchronized statements. Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock:For example:
public void addName(String name) {
synchronized(this) {lastName = name;nameCount++;}nameList.add(name);}
In this example, the addName method needs to synchronize changes to lastName and nameCount, but also needs to avoid synchronizing invocations of other objects’ methods. Without synchronized statements, there would have to be a separate, unsynchronized method for the sole purpose of invoking nameList.add.
Reference: The Java Tutorial,Intrinsic Locks and Synchronization

Freek

Freek

Answer is BCDG

It i NOT F because the F synchronizes on this, which refers to the WorkerThread instance. So both threads would be locking synchronizing on their own reference, as a result still allowing concurrent access.
G works because the object bar is static, so both threads refer to the same object.

garymar

garymar

the answer is BCG

D is incorrect because as a lock, list is also modified. It will double the size of the list and all the elements are not in order.