62,621
社区成员
发帖
与我相关
我的任务
分享
1
Runnable r = new Runnable() {
public void run() {
System.out.print("Cat");
}
};
Threadt=new Thread(r) {
public void run() {
System.out.print("Dog");
}
};
t.start();
What is the result?
Copyright Tarena Corporation,2008.All rights reserved
A. Cat
B. Dog
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Answer: B
Question 3
Given:
public class Threads3 implements Runnable {
public void run() {
System.out.print("running");
}
public static void main(String[] args) {
Thread t = new Thread(new Threads3());
t.run();
t.run();
t.start();
}
}
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints "running".
D. The code executes and prints "runningrunning".
E. The code executes and prints "runningrunningrunning".
Answer: E
Question 4
Click the Exhibit button:
public class Threads 1 {
Int x=0;
public class Runner implements Runnable {
public void run() {
int current = 0;
for(int i=0;i<4;i++){
current = x;
System.out.print(current + ", ");
x = current + 2;
}
}
}
public static void main(String[] args) {
Copyright Tarena Corporation,2008.All rights reserved
new Threads1().go();
}
public void go() {
Runnable r1 = new Runner();
new Thread(r1).start();
new Thread(r1 ).start();
}
}
Which two are possible results? (Choose two.)
A. 0, 2, 4, 4, 6, 8, 10, 6,
B. 0, 2, 4, 6, 8, 10, 2, 4,
C. 0, 2, 4, 6, 8, 10, 12, 14,
D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
Answer: AC
import java.util.*;
class NameList {
private List names = new ArrayList();
public synchronized void add(String name) {
names.add(name);
}
public synchronized void printAll() {
for (int i = 0; i < names.size(); i++) {
System.out.print(names.get(i) + " ");
}
}
public static void main(String[] args) {
final NameList sl = new NameList();
for (int i = 0; i < 2; i++) {
new Thread() {
public void run() {
sl.add("A");
sl.add("B");
sl.add("C");
sl.printAll();
}
}.start();
}
}
}
class Threads4 {
public static void main(String[] args) {
new Threads4().go();
}
public void go() {
Runnable r = new Runnable() {
public void run() {
System.out.print("foo");
}
};
Thread t = new Thread(r);
t.start();
t.start();
}
}
public class Transfers {
public static void main(String[] args) throws Exception {
Record r1 = new Record();
Record r2 = new Record();
doTransfer(r1, r2, 5);
doTransfer(r2, r1, 2);
doTransfer(r1, r2, 1);
// print the result
System.out.println("rl = " + r1.get() +", r2=" + r2.get());
}
private static void doTransfer(
final Record a, final Record b, final int amount) {
Thread t = new Thread() {
public void run() {
new Clerk().transfer(a, b, amount);
}
};
t.start();
}
}
class Clerk {
public synchronized void transfer(Record a, Record b, int amount){
synchronized (a) {
synchronized (b) {
a.add(-amount);
b.add(amount);
}
}
}
}
class Record {
int num=10;
public int get() { return num; }
public void add(int n) { num = num + n; }
}
