关于并发编程的相关问题的请教
package concurrency;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class OperateThreadSyn implements Runnable{
private int a = 0;
private int b = 0;
public int getA() {
return a;
}
public int getB() {
return b;
}
void operateFields(){
a++;
Thread.yield();
b++;
}
@Override
public void run(){
while((getB() + getA()) % 2 == 0){
System.out.println(getA() + "," + getB());
operateFields();
}
System.out.println("不是偶数,执行失败了!!");
}
public static void main(String[] args){
OperateThreadSyn ots = new OperateThreadSyn();
ExecutorService exec = Executors.newCachedThreadPool();
for(int i = 0 ; i < 3 ;i++){
exec.execute(ots);
}
exec.shutdown();
}
}
//=================上面的这个程序为何执行了半天也没有出现我想要的结果,
//=====我想要的结果就是看到这句的执行 System.out.println("不是偶数,执行失败了!!");
//但是我一直都没有看到
//===但是如果是把一个句子删除 得到如下的程序 则能够满足我的要求 求大神解释
package concurrency;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class OperateThreadSyn implements Runnable{
private int a = 0;
private int b = 0;
public int getA() {
return a;
}
public int getB() {
return b;
}
void operateFields(){
a++;
Thread.yield();
b++;
}
@Override
public void run(){
while((getB() + getA()) % 2 == 0){
// System.out.println(getA() + "," + getB());
operateFields();
}
System.out.println("不是偶数,执行失败了!!");
}