notice!
maybe the answer is not right.It seems D is right.
public class TestSync extends Thread
{
SyncTest syncTest;
public TestSync(String name)
{
super(name);
syncTest = new SyncTest();
}
public static void main(String[] arg)
{
Thread ts1 = new TestSync("A");
Thread ts2 = new TestSync("B");
ts1.start();
ts2.start();
}
public void run()
{
for(int i=0;i<10;i++)
{
syncTest.setXY(i);
System.out.println(syncTest.check()+":"+getName());
try {
Thread.sleep(10);
}catch(Exception e)
{
throw new RuntimeException("interrupte!");
}
}
}
}
maybe the answer is not right.It seems D is right.
public class TestSync extends Thread
{
SyncTest syncTest;
public TestSync(String name)
{
super(name);
syncTest = new SyncTest();
}
public static void main(String[] arg)
{
Thread ts1 = new TestSync("A");
Thread ts2 = new TestSync("B");
ts1.start();
ts2.start();
}
public void run()
{
for(int i=0;i<10;i++)
{
syncTest.setXY(i);
System.out.println(syncTest.check()+":"+getName());
try {
Thread.sleep(10);
}catch(Exception e)
{
throw new RuntimeException("interrupte!");
}
}
}
}
thread 2 在thread 1执行完setX后(此时setY还未执行完),执行setX此时,setX的值为thread 2执行的结果,setY的值为thread 1执行的结果。当然结果为true了。
而且不必多个线程调用setXY(),一个就够了。grgh2000(太阳)提供例程是错的。
private int x;
private int y; public synchronized void setX (int i) {x=i;}
public synchronized void setY (int i) {y=i;} public void setXY(int i){
setX(i);
setY(i);
} public synchronized boolean check() {return x!=y;}
} class Setter implements Runnable{
private SharedVar sv;
Setter(SharedVar sv){
this.sv=sv;
} public void run(){
while(true){
sv.setXY((int)(Math.random()*100));
}
}
}
class Checker implements Runnable{
private SharedVar sv;
Checker(SharedVar sv){
this.sv=sv;
} public void run(){
while(true){
if(sv.check())
System.out.println("\nx!=y: true");
else System.out.print(".");
}
}
}public class SyncTest{
public static void main(String[] args){
SharedVar sv=new SharedVar();
new Thread(new Setter(sv)).start();
new Thread(new Checker(sv)).start();
}
}
class SharedVar{
private int x;
private int y; public synchronized void setX (int i) {
try{
Thread.currentThread().sleep(10000);//延长运行时间.
}catch(Exception e){}
x=i;}
public synchronized void setY (int i) {
try{
Thread.currentThread().sleep(10000);
}catch(Exception e){}
y=i;
} public void setXY(int i){
setX(i);
setY(i);
} public synchronized boolean check() {return x!=y;}
}
private int x;
private int y; public synchronized void setX (int i) {
try
{
Thread.sleep((int)(Math.random()*100));
}
catch (Exception e)
{throw new RuntimeException("interrupte!");}
x=i;
}
public synchronized void setY (int i) {y=i;} public synchronized void setXY(int i){
setX(i);
setY(i);
} public boolean check() {return x!=y;}
} class a implements Runnable{
private SharedVar sv;
a(SharedVar sv){
this.sv=sv;
} public void run(){
while(true){
sv.setXY((int)(Math.random()*100));
if(sv.check())
System.out.println("\nx!=y: true");
else System.out.print(".");
}
}
}public class SyncTest1{
public static void main(String[] args){
SharedVar sv=new SharedVar();
Thread a_thread1 = new Thread(new a(sv));
a_thread1.start();
Thread a_thread2 = new Thread(new a(sv));
a_thread2.start();
}
}方案二是创建两个不同的类实现runnable接口或extends thread ,然后分别创建那两个类的一条线程。这时候,它们有不同的run()方法,如下所示:(修改miwoo的例程)class SharedVar{
private int x;
private int y; public synchronized void setX (int i) {x=i;}
public synchronized void setY (int i) {y=i;} public synchronized void setXY(int i){
setX(i);
setY(i);
} public boolean check() {return x!=y;}
} class a implements Runnable{
private SharedVar sv;
a(SharedVar sv){
this.sv=sv;
} public void run(){
while(true){
sv.setXY((int)(Math.random()*100));
}
}
}
class b implements Runnable{
private SharedVar sv;
b(SharedVar sv){
this.sv=sv;
} public void run(){
while(true){
if(sv.check())
System.out.println("\nx!=y: true");
else System.out.print(".");
}
}
}public class SyncTest2{
public static void main(String[] args){
SharedVar sv=new SharedVar();
Thread a_thread1 = new Thread(new a(sv));
a_thread1.start();
Thread b_thread1 = new Thread(new b(sv));
b_thread1.start();
}
}
方案一分析:(例程如下)1、本方案两条线程(a_thread1和a_thread2)均为同一个类(a)的对象。
2、因此具有同一个run()方法。
3、check()方法与setXY()方法在同一个run()方法里面。
4、从流程图可以看到,在同一条线程里面(a_thread1或a_thread2),由于check()要等setX()与setY()方法运行完后再执行,因此,check()方法永远是false!
5、但是,当同时考虑两条线程时,情况y就不同了:由于setXY()没有synchronized,因此,假设当线程a_thread1执行完setX()方法后,刚好轮到线程a_thread2执行check()方法,这时候就有可能出现x!=y了!!但本人在实际调试过程中,始终是false!也就是说x始终等于y!为什么呢???我已经用sleep()方法设法错开setX()与setY(),以便让另外一条线程执行它的check()方法,但根本不起作用!难道变量x与y不能在两条线程之间共享数据?线程会自动保护自己的x与y??方案二分析:
1、本方案两条线程(a_thread1和b_thread1)分别是类(a)及类(b)的对象。
2、因此分别具有不同的run()方法。
3、check()方法与setXY()分别在不同的run()方法里面。
4、由流程图可知:由于setXY()方法没有synchronized,因此当线程a_thread1在运行完setX()后,但未运行setY()的时候,线程b_thread1有可能会调用check()方法,此时,x与y的值就可能不相同!!
请各路前辈帮忙给点意见!!小弟感激不尽!!