当前位置: 技术问答>java相关
请解释这个线程的问题,讲讲它的执行过程!
来源: 互联网 发布时间:2017-03-25
本文导语: class s implements Runnable{ static int x=0,y=0; synchronized static void addX(){x++; System.out.println("IN addX()"); } synchronized static void...
class s implements Runnable{
static int x=0,y=0;
synchronized static void addX(){x++;
System.out.println("IN addX()");
}
synchronized static void addY(){y++;
System.out.println("IN addY()");
}
static 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()
B call addY() and addX() simultaneously for number of times in run()
C all addXY() for number of times in run()
为什么答案是B,C呢?
可我运行后怎么总是false呢?
static int x=0,y=0;
synchronized static void addX(){x++;
System.out.println("IN addX()");
}
synchronized static void addY(){y++;
System.out.println("IN addY()");
}
static 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()
B call addY() and addX() simultaneously for number of times in run()
C all addXY() for number of times in run()
为什么答案是B,C呢?
可我运行后怎么总是false呢?
|
c
因为addXY() 方法没有被同步。所以极有可能在运行玩x++ 后,被其他的程序打断。。。。。。
因为addXY() 方法没有被同步。所以极有可能在运行玩x++ 后,被其他的程序打断。。。。。。
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。