用Java代码模拟实现:一个人不断往箱子里放苹果,另一个人不断从箱子里取苹果,箱子里只能放5个苹

Python08

用Java代码模拟实现:一个人不断往箱子里放苹果,另一个人不断从箱子里取苹果,箱子里只能放5个苹,第1张

package com.zt.test

import java.util.Stack

public class Test7 {

private Stack<String> box

public String operationBox(boolean flag, String num) {

String str = ""

synchronized (Test7.class) {

if (flag) {

this.box.push(num)

} else {

str = this.box.pop()

}

}

return str

}

public static void main(String args[]) {

Test7 a = new Test7()

a.initApples()

}

public Test7() {

this.box = new Stack<String>()

}

public void forSleep(int i) {

try {

Thread.sleep(i)

} catch (Exception e) {

e.printStackTrace()

}

}

/**

 * 放苹果的人

 */

public void putApple() {

int no = 1

while (true) {

this.forSleep(1000)

int check = this.box.size()

System.out.println("---------现在有" + check + "个苹果---------")

if (check >= 5) {

System.out.println("------------箱子里有5个苹果无法放入------------")

continue

} else {

System.out

.println("------------放入第" + no + "个苹果--------------")

this.operationBox(true, no + "")

no = no + 1

}

}

}

/**

 * 拿苹果的人

 */

public void getApple() {

while (true) {

this.forSleep(800)

int check = this.box.size()

System.out.println("=========现在有" + check + "个苹果============")

if (check == 0) {

System.out.println("==========箱子里有0个苹果无法去除===========")

continue

} else {

String str = this.operationBox(false, null)

System.out.println("==========从箱子出取出第" + str + "个苹果==========")

}

}

}

/*

 * 初始化两个人

 */

public void initApples() {

Thread t1 = new Thread(new Runnable() {

public void run() {

try {

putApple()

} catch (Exception e) {

}

}

})

t1.start()

Thread t2 = new Thread(new Runnable() {

public void run() {

try {

getApple()

} catch (Exception e) {

}

}

})

t2.start()

}

}

一个放一个拿,operationBox()锁住stack的操作,forsleep方法是进行休眠的,模拟一块一慢,如果需要无序时间,自己写随机函数进行线程休眠。

需要使用多线程

package org.xuyh.design

import java.util.Scanner

/**

 * N个苹果放到20个袋子中,操作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟

 * @author XuYanhang

 *

 */

public class AppleOperation implements Runnable{

public static final int ONE_SECOND = 1000

private final int[] packages

public final int operationTime

public final int minSpacetTime

private int appleCount

private boolean timeEnd = false

public AppleOperation(int appleCount,int packCount,int operationTime,int minSpacetTime){

  this.appleCount = appleCount

  packages = new int[packCount]

  this.operationTime = operationTime

  this.minSpacetTime = minSpacetTime

 }

public int getAppleCount(){

  return appleCount

 }

public int getPackCount(){

  return packages.length

 }

public int getOperationTime(){

  return operationTime

 }

public boolean hasEnd(){

  return timeEnd || appleCount == 0

 }

public synchronized void setTimeEnd(){

  timeEnd = true

 }

public void run() {

  int pack = Integer.parseInt(Thread.currentThread().getName())

  while(appleCount>0){

   synchronized(this){

    if(hasEnd()){

     break

    }

    appleCount--

    packages[pack]++

    System.out.println("一个苹果放入了第"+(pack+1)+"个袋子中,还剩"+appleCount+"个苹果")

   }

   for(int i = 0 i <minSpacetTime i++){

    synchronized(this){

     if(hasEnd()){

      break

     }

    }

    try {

     Thread.sleep(ONE_SECOND)

    } catch (InterruptedException e) {

     e.printStackTrace()

    }

   }

  }

  synchronized(this){

   System.out.println("第"+(pack+1)+"个袋子最终有"+packages[pack]+"个苹果")

  }

 }

//Test main method

 public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in)

  

  int appleCount = -1

  do{

   System.out.print("N个苹果放到20个袋子中,操作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟。\n"

     + "输入苹果总数N:")

   String string = scanner.next()

   if(null != string &&string.matches("^\\d{1,8}$")){

    appleCount = Integer.parseInt(string)

   }

  }while(appleCount == -1)

  

  scanner.close()

AppleOperation operation = new AppleOperation(appleCount,20,4*60*60,30*60)

new WaitThread(operation).start()

for (int i = 0i <operation.getPackCount()i++) {

   

   Thread thread = new Thread(operation,""+i)

   

   thread.start()

   

  }

  

 }

static class WaitThread extends Thread{

private AppleOperation operation

public WaitThread(AppleOperation operation){

   this.operation = operation

  }

  

  public void run(){

   try {

    Thread.sleep(operation.getOperationTime()*ONE_SECOND)

   } catch (InterruptedException e) {

    e.printStackTrace()

   }

   

   operation.setTimeEnd()

System.out.println("时间到,苹果装袋操作结束,最后为装袋的苹果个数:"+operation.getAppleCount())

}

 }

}

这里我们额外启动了21个线程,其中一个是计4小时的定时的,其他20个是向每个袋子中放苹果的线程。

插板法

C(M+1)

(N-1)

比如M=4,N=3

则C5

2=10

就是说把M个苹果摆成一排,其间及两边总共(M+1)个位置

【原因是篮子可空.

若题目要求至少一个苹果,则两边不能放板,只能在其间期间插板,数目(M-1)】

任插(N-1)个板,两板之间为一个篮子,则有N个篮子

咱们只需要知道有多少种插法,就可以知道有多少种放法

即C(M+1)

(N-1)

(篮子相同,此处应该不排序)

看怎样,