线程 SCJP

givemeasky 2009-11-19 04:24:45
今天看了些线程的scjp题 好混乱觉得 希望高手能帮解释下答案

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

























...全文
219 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjjk123 2009-11-24
  • 打赏
  • 举报
回复
MARK
givemeasky 2009-11-24
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 still_rain 的回复:]
引用 12 楼 givemeasky 的回复:
Java codeimport java.util.*;class NameList {private List names=new ArrayList();publicsynchronizedvoid add(String name) {
        names.add(name);
    }publicsynchronizedvoid printAll() {for (int i=0; i < names.size(); i++) {
            System.out.print(names.get(i)+"");
        }
    }publicstaticvoid main(String[] args) {final NameList sl=new NameList();for (int i=0; i <2; i++) {new Thread() {publicvoid run() {
                    sl.add("A");
                    sl.add("B");
                    sl.add("C");
                    sl.printAll();
                }
            }.start();
        }
    }
}
Which two statements are true if this class is compiled and run?
(Choose two.)
A. An exception may be thrown at runtime.
B. The code may run with no output, without exiting.
C. The code may run with no output, exiting normally.
D. The code may rum with output "A B A B C C ", then exit.
E. The code may rum with output "A B C A B C A B C ", then exit.
F. The code may ruin with output "A A A B C A B C C ", then exit.
G. The code may ruin with output "A B C A A B C A B C ", then exit.
Answer: EG
  why?


这个跟你在开贴问的Q4类似,可以看楼上的关于Q4的回答。
[/Quote]

但是 那个两个线程 这个是一个线程啊
阿士匹灵 2009-11-21
  • 打赏
  • 举报
回复
呵呵

学习
爱摸鱼de老邪 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 givemeasky 的回复:]
Java codeimport java.util.*;class NameList {private List names=new ArrayList();publicsynchronizedvoid add(String name) {
names.add(name);
}publicsynchronizedvoid printAll() {for (int i=0; i< names.size(); i++) {
System.out.print(names.get(i)+"");
}
}publicstaticvoid main(String[] args) {final NameList sl=new NameList();for (int i=0; i<2; i++) {new Thread() {publicvoid run() {
sl.add("A");
sl.add("B");
sl.add("C");
sl.printAll();
}
}.start();
}
}
}
Which two statements are true if this class is compiled and run?
(Choose two.)
A. An exception may be thrown at runtime.
B. The code may run with no output, without exiting.
C. The code may run with no output, exiting normally.
D. The code may rum with output "A B A B C C ", then exit.
E. The code may rum with output "A B C A B C A B C ", then exit.
F. The code may ruin with output "A A A B C A B C C ", then exit.
G. The code may ruin with output "A B C A A B C A B C ", then exit.
Answer: EG
why?

[/Quote]
这个跟你在开贴问的Q4类似,可以看楼上的关于Q4的回答。
givemeasky 2009-11-20
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 still_rain 的回复:]
Thread类的start方法不能多次调用,否则会抛出一个IllegalThreadStateException异常。
[/Quote]
谢谢 在API中查到了
givemeasky 2009-11-20
  • 打赏
  • 举报
回复

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();
}
}
}


Which two statements are true if this class is compiled and run?
(Choose two.)
A. An exception may be thrown at runtime.
B. The code may run with no output, without exiting.
C. The code may run with no output, exiting normally.
D. The code may rum with output "A B A B C C ", then exit.
E. The code may rum with output "A B C A B C A B C ", then exit.
F. The code may ruin with output "A A A B C A B C C ", then exit.
G. The code may ruin with output "A B C A A B C A B C ", then exit.
Answer: EG
why?
爱摸鱼de老邪 2009-11-20
  • 打赏
  • 举报
回复
Thread类的start方法不能多次调用,否则会抛出一个IllegalThreadStateException异常。
givemeasky 2009-11-20
  • 打赏
  • 举报
回复

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();
}
}



What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "foo".
D. The code executes normally, but nothing is printed.
Answer: B

givemeasky 2009-11-20
  • 打赏
  • 举报
回复

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; }
}


If Transfers.main() is run, which three are true? (Choose three.)
A. The output may be "r1 = 6, r2 = 14".
B. The output may be "r1 = 5, r2 = 15".
C. The output may be "r1 = 8, r2 = 12".
D. The code may run (and complete) with no output.
E. The code may deadlock (without completing) with no output.
F. M IllegalStateException or InterruptedException may be thrown at
runtime.
Answer: ABE

老张-AI 2009-11-19
  • 打赏
  • 举报
回复

路过 学习了

顶下
godismydaughter 2009-11-19
  • 打赏
  • 举报
回复
我也来接接分
晴天1999 2009-11-19
  • 打赏
  • 举报
回复
新手,多多指教哟!
fyofmee 2009-11-19
  • 打赏
  • 举报
回复
2楼说得挺详细的,帮顶下
爱摸鱼de老邪 2009-11-19
  • 打赏
  • 举报
回复
该说的都说了,我是来接分滴~~~~~
苍蝇①号 2009-11-19
  • 打赏
  • 举报
回复
1、2题没什么说的,第3题2楼的已经说的很详细了
wifewifewife 2009-11-19
  • 打赏
  • 举报
回复
bendanlzh 2009-11-19
  • 打赏
  • 举报
回复
最后一问变态啊!
排除法,两个线程一共打印8次,故d,e错,现在3选2

答案A,其中一个线程打印先执行3次循环,打印出0,2,4,在第三次执行完System.out.print(current + ", ");后,执行时间到(此时局部变量current=4,x=4)
第二个线程来连续执行4次就打印出4,6,8,10。
再第一个线程执行,因为在一个线程中current=4,执行完x=current+2后x=6.所以最后打印出6.

答案C。很简单,两个线程循环轮流执行就是这个答案

其它的题很简单,看下Thread类源码,您就什么都明白了。或者是等楼下的答案吧
givemeasky 2009-11-19
  • 打赏
  • 举报
回复
可怜啊 没人理我

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧