多项选择题

public class NamedCounter { 
private final String name; 
private int count; 
public NamedCounter(String name) { this.name = name; } 
public String getName() { return name; } 
public void increment() { coount++; } 
public int getCount() { return count; }
public void reset() { count = 0; } } 
Which three changes should be made to adapt this class to be used safely by multiple threads? ()

A. declare reset() using the synchronized keyword
B. declare getName() using the synchronized keyword
C. declare getCount() using the synchronized keyword
D. declare the constructor using the synchronized keyword
E. declare increment() using the synchronized keyword

相关考题

public class TwoThreads {  private static Object resour...


单项选择题

public class TwoThreads { 
private static Object resource = new Object(); 
private static void delay(long n) { 
try { Thread.sleep(n); } 
catch (Exception e) { System.out.print(”Error “); } 
} 
public static void main(String[] args) { 
System.out.print(”StartMain “); 
new Thread1().start(); 
delay(1000); 
Thread t2 = new Thread2(); 
t2.start(); 
delay(1000); 
t2.interrupt 
delay(1000); 
System.out.print(”EndMain “); 
} 
static class Thread 1 extends Thread { 
public void run() { 
synchronized (resource) { 
System.out.print(”Startl “); 
delay(6000); 
System.out.print(”End1 “); 
} 
} 
} 
static class Thread2 extends Thread { 
public void run() { 
synchronized (resource) { 
System.out.print(”Start2 “); 
delay(2000); 
System.out.print(”End2 “); 
} 
} 
} 
} 
Assume that sleep(n) executes in exactly m milliseconds, and all other code executes in an insignificant amount of time. What is the output if the main() method is run?() 

A. Compilation fails.
B. Deadlock occurs.
C. StartMain Start1 Error EndMain End1
D. StartMain Start1 EndMain End1 Start2 End2
E. StartMain Start1 Error Start2 EndMain End2 End1
F.StartMain Start1 EndMain End1 Start2 Error End2