1.一个生产者和一个消费者。
存在资源竞争情况:1)生产者和消费者同时访问fruit store的库存量,及同时修改库存量时,存在资源竞争。
细分这种资源竞争的情况,至少会有如下两种原因:1)当时fruit store库存为0时,还未成功执行this.curFruitNum++,消费者发现fuilt store发现库存数量还是为0,无法购买产品;当fuilt store库存为100时,还未成功执行this.curFruitNum--,生产者发现库存量还是100,不再生产产品。这种资源竞争的情况,如果对于某一次临界值库存100或0判断,还未执行curF错误,还不算糟糕,虽然造成了某一次的判断错误,但并没有影响整体业务功能。2)还有一种极端的情况,生产者和消费者同时执行this.curFruitNum++和this.curFruitNum--,由于++和--非原子性操作,会导致结果和预期不同,这种情况会影响业务功能。
class FruitStore {
public int curFruitNum = 0;
public boolean increase()
{
if (curFruitNum < 100) {
this.curFruitNum++;
return true;
}
return false;
}
public boolean decrease()
{
if (curFruitNum > 0) {
this.curFruitNum--;
return true;
}
return false;
}
}
class FruitWorkerTom implements Runnable {
private FruitStore fruitStore;
private int stockNum = 0;
public FruitWorkerTom(FruitStore fruitStore)
{
this.fruitStore = fruitStore;
}
public boolean stockFruit() {
if (this.fruitStore.increase())
{
System.out.println("stock");
return true;
}
return false;
}
@Override
public void run() {
while (this.stockNum < 100) {
if (this.stockFruit()) {
this.stockNum++;
Thread.yield();
}
}
}
}
class FruitBuyerMary implements Runnable {
private FruitStore fruitStore;
private int buyNum = 0;
public FruitBuyerMary(FruitStore fruitStore)
{
this.fruitStore = fruitStore;
}
public boolean buyFruit() {
if (this.fruitStore.decrease()) {
System.out.println("buy");
return true;
}
return false;
}
@Override
public void run() {
while (this.buyNum < 100) {
if (this.buyFruit()) {
this.buyNum++;
Thread.yield();
}
}
}
}
public class MainThread {
public static void main(String[] args) {
FruitStore fruitStore = new FruitStore();
Thread fruitWorkerTom = new Thread(new FruitWorkerTom(fruitStore));
FruitWorkerTom.start();
Thread fruitBuyerMary = new Thread(new FruitBuyerMary(fruitStore));
FruitBuyerMary.start();
try {
fruitWorkerTom.join();
fruitBuyerMary.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(fruitStore.curFruitNum);
}
}
1.多个生产者和多个消费者。
存在资源竞争情况:1)生产者和消费者同时访问fruit store的库存量,及同时修改库存量时,存在资源竞争。2)生产者和生产者同时访问fruit store的库存量,及同时修改库存量时,存在资源竞争。2)消费者和消费者同时访问fruit store的库存量,及同时修改库存量时,存在资源竞争。
class FruitStore {
public int curFruitNum = 0;
public boolean increase()
{
if (curFruitNum < 100000) {
this.curFruitNum++;
return true;
}
return false;
}
public boolean decrease()
{
if (curFruitNum > 0) {
this.curFruitNum--;
return true;
}
return false;
}
}
class FruitWorkerTom implements Runnable {
private FruitStore fruitStore;
public int stockNum = 0;
public FruitWorkerTom(FruitStore fruitStore)
{
this.fruitStore = fruitStore;
}
public boolean stockFruit() {
if (this.fruitStore.increase())
{
System.out.println("Tom stock");
return true;
}
return false;
}
@Override
public void run() {
while (this.stockNum < 10000) {
if (this.stockFruit()) {
this.stockNum++;
System.out.println(this.stockNum);
Thread.yield();
}
}
}
}
class FruitWorkerBob implements Runnable {
private FruitStore fruitStore;
public int stockNum = 0;
public FruitWorkerBob(FruitStore fruitStore)
{
this.fruitStore = fruitStore;
}
public boolean stockFruit() {
if (this.fruitStore.increase())
{
System.out.println("Bob stock");
return true;
}
return false;
}
@Override
public void run() {
while (this.stockNum < 10000) {
if (this.stockFruit()) {
this.stockNum++;
System.out.println(this.stockNum);
Thread.yield();
}
}
}
}
class FruitBuyerMary implements Runnable {
private FruitStore fruitStore;
public int buyNum = 0;
public FruitBuyerMary(FruitStore fruitStore)
{
this.fruitStore = fruitStore;
}
public boolean buyFruit() {
if (this.fruitStore.decrease()) {
System.out.println("buy");
return true;
}
return false;
}
@Override
public void run() {
while (this.buyNum < 10000) {
if (this.buyFruit()) {
this.buyNum++;
System.out.println(this.buyNum);
Thread.yield();
}
}
}
}
class FruitBuyerLucy implements Runnable {
private FruitStore fruitStore;
public int buyNum = 0;
public FruitBuyerLucy(FruitStore fruitStore)
{
this.fruitStore = fruitStore;
}
public boolean buyFruit() {
if (this.fruitStore.decrease()) {
System.out.println("buy");
return true;
}
return false;
}
@Override
public void run() {
while (this.buyNum < 10000) {
if (this.buyFruit()) {
this.buyNum++;
System.out.println(this.buyNum);
Thread.yield();
}
}
}
}
public class MainThread {
public static void main(String[] args) {
FruitStore fruitStore = new FruitStore();
Thread fruitWorkerTom = new Thread(new FruitWorkerTom(fruitStore));
fruitWorkerTom.start();
Thread fruitBuyerMary = new Thread(new FruitBuyerMary(fruitStore));
fruitBuyerMary.start();
Thread fruitWorkerBob= new Thread(new FruitWorkerBob(fruitStore));
fruitWorkerBob.start();
Thread fruitBuyerLucy = new Thread(new FruitBuyerLucy(fruitStore));
fruitBuyerLucy.start();
try {
fruitWorkerTom.join();
fruitBuyerMary.join();
fruitWorkerBob.join();
fruitBuyerLucy.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(fruitStore.curFruitNum);
}
}