当前位置: 技术问答>java相关
所有考过SCJP的人来看看这三道线程题!!!!!!!!!!
来源: 互联网 发布时间:2015-03-31
本文导语: //你们能给出一定正确的答案吗?并且能解释一下吗? 3. Given public class S implements Runnable { int x=0,y=0; synchronized void addX() { x++; } synchronized void addY() { y++; ...
//你们能给出一定正确的答案吗?并且能解释一下吗?
3. Given
public class S implements Runnable
{
int x=0,y=0;
synchronized void addX()
{
x++;
}
synchronized void addY()
{
y++;
}
void addXY()
{
x++;
y++;
}
boolean check()
{
return (x>y)?true:false;
}
public void run()
{
System.out.println(check());
}
public static void main(String args[])
{
S run=new S();
Thread t1=new Thread(run);
Thread t2=new Thread(run);
t1.start();
t2.start();
}
}
If this methods are called in which order the check will return true? Select all that apply
A. call addX() and addY() simultaneously for number of
times in run().
A. call addY() and addX() simultaneously for number of times in run().
B. call addXY() for number of times in run().
Answer:
4. Click the exhibit button:
1. public class X implements Runnable{
2. private int x;
3. private int y;
4.
5. public static void main(String[] args) {
6. X that = new X();
7. (new Thread(that)).start();
8. (new Thread(that)).start();
9. }
10.
11. public void run() {
12. for (;;) {
13. x++;
14. y++;
15. System.out.println(“x=” + x + “, y = ” + y);
16. }
17. }
18. }
What is the result?
A. Errors at lines 7 and 8 cause compilation to fail.
B. The program prints pairs of values for x and y that might not always be the same on the same line (for example, “x=2, y=1”).
C. The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears twice (for example, “x=1, y=1” followed by “x=1, y=1”).
D. The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears only for once (for example, “x=1, y=1” followed by “x=2, y=2”).
Answer:
5. Given:
class Happy implements Runnable
{
private int x;
private int y;
public synchronized void run()
{
x++;
y++;
System.out.println(x+" "+y);
}
public static void main(String args[])
{
Happy that=new Happy();
(new Thread(that)).start();
(new Thread(that)).start();
}
}
What happens when this code compiles and run?
A. will print x ,y in order 11 22
B. will print x ,y twice in order 11 22 11 22
C. will print x ,y in order 12 12
D. will print x ,y order is unpredictable.
E. Compilation error.
F. Runtime Exception.
Answer:
3. Given
public class S implements Runnable
{
int x=0,y=0;
synchronized void addX()
{
x++;
}
synchronized void addY()
{
y++;
}
void addXY()
{
x++;
y++;
}
boolean check()
{
return (x>y)?true:false;
}
public void run()
{
System.out.println(check());
}
public static void main(String args[])
{
S run=new S();
Thread t1=new Thread(run);
Thread t2=new Thread(run);
t1.start();
t2.start();
}
}
If this methods are called in which order the check will return true? Select all that apply
A. call addX() and addY() simultaneously for number of
times in run().
A. call addY() and addX() simultaneously for number of times in run().
B. call addXY() for number of times in run().
Answer:
4. Click the exhibit button:
1. public class X implements Runnable{
2. private int x;
3. private int y;
4.
5. public static void main(String[] args) {
6. X that = new X();
7. (new Thread(that)).start();
8. (new Thread(that)).start();
9. }
10.
11. public void run() {
12. for (;;) {
13. x++;
14. y++;
15. System.out.println(“x=” + x + “, y = ” + y);
16. }
17. }
18. }
What is the result?
A. Errors at lines 7 and 8 cause compilation to fail.
B. The program prints pairs of values for x and y that might not always be the same on the same line (for example, “x=2, y=1”).
C. The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears twice (for example, “x=1, y=1” followed by “x=1, y=1”).
D. The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears only for once (for example, “x=1, y=1” followed by “x=2, y=2”).
Answer:
5. Given:
class Happy implements Runnable
{
private int x;
private int y;
public synchronized void run()
{
x++;
y++;
System.out.println(x+" "+y);
}
public static void main(String args[])
{
Happy that=new Happy();
(new Thread(that)).start();
(new Thread(that)).start();
}
}
What happens when this code compiles and run?
A. will print x ,y in order 11 22
B. will print x ,y twice in order 11 22 11 22
C. will print x ,y in order 12 12
D. will print x ,y order is unpredictable.
E. Compilation error.
F. Runtime Exception.
Answer:
|
3.If this methods are called in which order the check will return true? Select all that apply
A. call addX() and addY() simultaneously for number of
times in run().
A. call addY() and addX() simultaneously for number of times in run().
B. call addXY() for number of times in run().
我认为Answer: call addXY() for number of times in run().
因为addX() and addY() 方法前有synchronized关键词进行保护,所以能使得数据一致性!
这个题的答案好象不完整把!
相关题目看看下面的:
Given:
public class SyncTest{
private int x;
private int y;
public void setX(int I){x=I;}
public void setY(int I){y=I;}
public synchronized void setXY(int I){setX(i);setY(i);}
public synchronized boolean check(){return x != y;}
}
Under which conditions will check() return true when called from adifferent class?
A. check() can never return true
B. check()can return true when setXY is called by multiple threads
C. check()can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SynchTest is changed to allow x and y to be set separately.
正确答案是:C!!!
4.正确为B!!!
5.正确为A!!!
解释见我的文档!
关于Thread的,很多人都说Thread考的多,而且难一点,可能我没遇到难题吧
Thread部分我没错
class a implements Runnable{
private int x;
private int y;
public void run(){
for(;;){
x++;
y++;
System.out.println(" x="+x+" y="+y);
System.out.println(当前Thread的名字);
}
}
}
class b{
public static void main(String[] args){
a aa = new a();
new Thread(aa).start();
new Thread(aa).start();
}
}
给几个选项,问你运行结果
补充:此题的正确答案是---有可能输出x!=y的结果并且输出显示的是两个线程在运行!
不会出现相同的x和y的值!
下面也是一道类似的题,你可以看看应该选哪个?
**************************************************
class Happy implements Runnable{
private int x;
private int y;
public synchronized void run() {
x++;
y++;
System.out.println(x+" "+y);
}
public static void main(String args[]) {
Happy that=new Happy();
(new Thread(that)).start();
(new Thread(that)).start();
}
}
What happens when this code compiles and run?
a) will print x ,y in order 11 22
b) will print x ,y twice in order 11 22 11 22
c) will print x ,y in order 12 12
d) will print x ,y order is unpredictable.
e) Compilation error.
f) Runtime Exception.
***********************************************************
6.非常碰巧,上面这道题改了一下又出现在我的考题里面
//前面一样
synchronized(this){
x++;
y++;
}
System.out.println...//后面一样
同样是给你几个选项,问运行结果
这个是同步的,上面那个有可能输出x!=y的结果,这个就不会了!
大家要注意5,6都只有一个aa,也就是不会出现相同的x或y.
补充:正确答案是----永远输出x=y并且输出显示的是两个线程在运行!
不会出现相同的x和y的值!
如果Thread的构造体中不仅仅是一个aa,也就是另外用类a再new一个实例bb,
并且new Thread(bb).start();的话,那运行结果就不一样了!就会出现相同的x和y的值!
(5和6我第一次考题都碰到了,第二次好象也有!SUN题库会经常出现的,祝你好运!)
大家可以把下面我提供的完整程序去认真的编译运行验证一下!
//大家将注释掉的和未注释的程序分别运行!
********************************************************
public class TestThread {
public static void main(String args[]) {
Xyz r = new Xyz();
//Xyz p = new Xyz();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
//Thread w1 = new Thread(p);
//Thread w2 = new Thread(p);
t1.setName("Thread_t1");
t2.setName("Thread_t2");
//w1.setName("Thread_w1");
//w2.setName("Thread_w2");
t1.start();
t2.start();
//w1.start();
//w2.start();
}
}
class Xyz implements Runnable {
private int i;
private int j;
public void run() {
//synchronized(this){
while(true) {
if ( i == 100 ) {
break;
}
System.out.println("i= " + i+++","+"j="+j++);
System.out.println(Thread.currentThread().getName());
}
//}
}
}
********************************************************
7.还有一个很经典的thread题,也是真题,
Thread1
synchronized(a){
synchronized(b){
}
}
Thread2
synchronized(b){synchronized(a){
}
}
都start();问结果,结果是--不确定的!可能deadlock,并且结果和当前Thread的执行机的环境有关!
就是大家很常见的那道题
a,b是StringBuffer();Thread1,2是用
new Thread(){
public void run(){
//....
}
}.start();
方式定义,一字不变。
真实的程序如下:
********************************************************
public class SyncTest
{
public static void main(String[] args)
{
final StringBuffer s1= new StringBuffer();
final StringBuffer s2= new StringBuffer();
new Thread ()
{
public void run()
{
synchronized(s1)
{
s1.append("A");
synchronized(s2)
{
s2.append("B");
System.out.print(s1);
System.out.print(s2);
}
}
}
}.start();
new Thread()
{
public void run()
{
synchronized(s2)
{
s2.append("C");
synchronized(s1)
{
s1.append("D");
System.out.print(s2);
System.out.print(s1);
}
}
}
}.start();
}
}
/*
Which two statements are true? (Choose Two)
A. The program prints "ABBCAD"
B. The program prints "CDDACB"
C. The program prints "ADCBADBC"
D. The output is a non-deterministic point because of a possible deadlock condition
E. The output is dependent on the threading model of the system the program is running on.
*/
********************************************************
正确的答案是:D和E!
根据程序流程和线程(同优先级)调用的不确定性,这个程序有可能会输出的是A和B.但是你应该注意到两个线程1和2锁定对象的时候的加锁顺序不一致,也就是说可能会导致死锁。
如果你实际去运行上面的程序,你会发现输出的结果可能总是"ABBCAD",实际上还可能输出"CDDACB"!这和你当前的JAVA虚拟机的运行环境有关的!你不能很好的加其他的Thread进来,所以看到的结果好象只有A答案!但是我的运行结果是--多半是A情况,B情况出现的几率少,呵呵!你,试试看?
这道题我两次考试都碰到了,你肯定也能碰到的!不信?我和你打赌,赌什么?当然是请我吃饭了,呵呵!
好象SUN的SCJP题库中关于Thread就没有其他的题型似的!当你遇到这个题时,答案都不用看,直接选D和E好了!
A. call addX() and addY() simultaneously for number of
times in run().
A. call addY() and addX() simultaneously for number of times in run().
B. call addXY() for number of times in run().
我认为Answer: call addXY() for number of times in run().
因为addX() and addY() 方法前有synchronized关键词进行保护,所以能使得数据一致性!
这个题的答案好象不完整把!
相关题目看看下面的:
Given:
public class SyncTest{
private int x;
private int y;
public void setX(int I){x=I;}
public void setY(int I){y=I;}
public synchronized void setXY(int I){setX(i);setY(i);}
public synchronized boolean check(){return x != y;}
}
Under which conditions will check() return true when called from adifferent class?
A. check() can never return true
B. check()can return true when setXY is called by multiple threads
C. check()can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SynchTest is changed to allow x and y to be set separately.
正确答案是:C!!!
4.正确为B!!!
5.正确为A!!!
解释见我的文档!
关于Thread的,很多人都说Thread考的多,而且难一点,可能我没遇到难题吧
Thread部分我没错
class a implements Runnable{
private int x;
private int y;
public void run(){
for(;;){
x++;
y++;
System.out.println(" x="+x+" y="+y);
System.out.println(当前Thread的名字);
}
}
}
class b{
public static void main(String[] args){
a aa = new a();
new Thread(aa).start();
new Thread(aa).start();
}
}
给几个选项,问你运行结果
补充:此题的正确答案是---有可能输出x!=y的结果并且输出显示的是两个线程在运行!
不会出现相同的x和y的值!
下面也是一道类似的题,你可以看看应该选哪个?
**************************************************
class Happy implements Runnable{
private int x;
private int y;
public synchronized void run() {
x++;
y++;
System.out.println(x+" "+y);
}
public static void main(String args[]) {
Happy that=new Happy();
(new Thread(that)).start();
(new Thread(that)).start();
}
}
What happens when this code compiles and run?
a) will print x ,y in order 11 22
b) will print x ,y twice in order 11 22 11 22
c) will print x ,y in order 12 12
d) will print x ,y order is unpredictable.
e) Compilation error.
f) Runtime Exception.
***********************************************************
6.非常碰巧,上面这道题改了一下又出现在我的考题里面
//前面一样
synchronized(this){
x++;
y++;
}
System.out.println...//后面一样
同样是给你几个选项,问运行结果
这个是同步的,上面那个有可能输出x!=y的结果,这个就不会了!
大家要注意5,6都只有一个aa,也就是不会出现相同的x或y.
补充:正确答案是----永远输出x=y并且输出显示的是两个线程在运行!
不会出现相同的x和y的值!
如果Thread的构造体中不仅仅是一个aa,也就是另外用类a再new一个实例bb,
并且new Thread(bb).start();的话,那运行结果就不一样了!就会出现相同的x和y的值!
(5和6我第一次考题都碰到了,第二次好象也有!SUN题库会经常出现的,祝你好运!)
大家可以把下面我提供的完整程序去认真的编译运行验证一下!
//大家将注释掉的和未注释的程序分别运行!
********************************************************
public class TestThread {
public static void main(String args[]) {
Xyz r = new Xyz();
//Xyz p = new Xyz();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
//Thread w1 = new Thread(p);
//Thread w2 = new Thread(p);
t1.setName("Thread_t1");
t2.setName("Thread_t2");
//w1.setName("Thread_w1");
//w2.setName("Thread_w2");
t1.start();
t2.start();
//w1.start();
//w2.start();
}
}
class Xyz implements Runnable {
private int i;
private int j;
public void run() {
//synchronized(this){
while(true) {
if ( i == 100 ) {
break;
}
System.out.println("i= " + i+++","+"j="+j++);
System.out.println(Thread.currentThread().getName());
}
//}
}
}
********************************************************
7.还有一个很经典的thread题,也是真题,
Thread1
synchronized(a){
synchronized(b){
}
}
Thread2
synchronized(b){synchronized(a){
}
}
都start();问结果,结果是--不确定的!可能deadlock,并且结果和当前Thread的执行机的环境有关!
就是大家很常见的那道题
a,b是StringBuffer();Thread1,2是用
new Thread(){
public void run(){
//....
}
}.start();
方式定义,一字不变。
真实的程序如下:
********************************************************
public class SyncTest
{
public static void main(String[] args)
{
final StringBuffer s1= new StringBuffer();
final StringBuffer s2= new StringBuffer();
new Thread ()
{
public void run()
{
synchronized(s1)
{
s1.append("A");
synchronized(s2)
{
s2.append("B");
System.out.print(s1);
System.out.print(s2);
}
}
}
}.start();
new Thread()
{
public void run()
{
synchronized(s2)
{
s2.append("C");
synchronized(s1)
{
s1.append("D");
System.out.print(s2);
System.out.print(s1);
}
}
}
}.start();
}
}
/*
Which two statements are true? (Choose Two)
A. The program prints "ABBCAD"
B. The program prints "CDDACB"
C. The program prints "ADCBADBC"
D. The output is a non-deterministic point because of a possible deadlock condition
E. The output is dependent on the threading model of the system the program is running on.
*/
********************************************************
正确的答案是:D和E!
根据程序流程和线程(同优先级)调用的不确定性,这个程序有可能会输出的是A和B.但是你应该注意到两个线程1和2锁定对象的时候的加锁顺序不一致,也就是说可能会导致死锁。
如果你实际去运行上面的程序,你会发现输出的结果可能总是"ABBCAD",实际上还可能输出"CDDACB"!这和你当前的JAVA虚拟机的运行环境有关的!你不能很好的加其他的Thread进来,所以看到的结果好象只有A答案!但是我的运行结果是--多半是A情况,B情况出现的几率少,呵呵!你,试试看?
这道题我两次考试都碰到了,你肯定也能碰到的!不信?我和你打赌,赌什么?当然是请我吃饭了,呵呵!
好象SUN的SCJP题库中关于Thread就没有其他的题型似的!当你遇到这个题时,答案都不用看,直接选D和E好了!