Java搬砖_求java 搬砖程序.

展开全部

有意思 呵呵 :

你参考一下吧:

功能是实现了 ,  但62616964757a686964616fe58685e5aeb931333330336339不知道合不合你胃口:

package main;

import java.util.ArrayList;

import java.util.List;

public class MoveBlock {

/**

* @param args

*/

public static void main(String[] args) {

List workers=new ArrayList();

RubPerson rubPerson1 = new RubPerson();

rubPerson1.setName("Person-1");

RubPerson rubPerson2 = new RubPerson();

rubPerson2.setName("Person-2");

RubPerson rubPerson3 = new RubPerson();

rubPerson3.setName("Person-3");

RubPerson rubPerson4 = new RubPerson();

rubPerson4.setName("Person-4");

RubPerson rubPerson5 = new RubPerson();

rubPerson5.setName("Person-5");

rubPerson1.setPoint(0);

rubPerson1.setNextRubPerson(rubPerson2);

rubPerson2.setPoint(1);

rubPerson2.setUpRubPerson(rubPerson1);

rubPerson2.setNextRubPerson(rubPerson3);

rubPerson3.setPoint(1);

rubPerson3.setUpRubPerson(rubPerson2);

rubPerson3.setNextRubPerson(rubPerson4);

rubPerson4.setPoint(1);

rubPerson4.setUpRubPerson(rubPerson3);

rubPerson4.setNextRubPerson(rubPerson5);

rubPerson5.setPoint(2);

rubPerson5.setUpRubPerson(rubPerson4);

rubPerson5.setNextRubPerson(rubPerson1);

workers.add(rubPerson1);

workers.add(rubPerson2);

workers.add(rubPerson3);

workers.add(rubPerson4);

workers.add(rubPerson5);

MyJob job=new MyJob(workers, rubPerson1);

job.setBlockCount(100);

new Thread(job).start();

}

}

class MyJob implements Runnable{

List workers=new ArrayList();

private int blockCount=0;

private RubPerson leaderPerson;

public List getWorkers() {

return workers;

}

public void setWorkers(List workers) {

this.workers = workers;

}

public int getBlockCount() {

return blockCount;

}

public void setBlockCount(int blockCount) {

this.blockCount = blockCount;

}

public RubPerson getLeaderPerson() {

return leaderPerson;

}

public void setLeaderPerson(RubPerson leaderPerson) {

this.leaderPerson = leaderPerson;

}

public MyJob(List workers, RubPerson leaderPerson) {

super();

this.workers = workers;

this.leaderPerson = leaderPerson;

}

public void run() {

while(this.getBlockCount()>=1){

leaderPerson.run();

this.setBlockCount(this.getBlockCount()-1);

try {

Thread.currentThread().sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

class RubPerson implements Runnable{

private String block = null;

private boolean isRest = true;

private RubPerson nextRubPerson = null;

private RubPerson upRubPerson = null;

private int point = 0;// 0 is start, 1 is middle, 2 is end;

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public void run() {

switch (this.getPoint()) {

case 0:

if(this.isRest()){

this.setBlock("Block");

printState(this.getPoint(), 1);

this.setRest(false);

}

try {

Thread.currentThread().sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

if (nextRubPerson != null) {

printState(this.getPoint(), 2);

nextRubPerson.run();

}

break;

case 1:

if(this.isRest()){

if(this.getUpRubPerson().isRest()){

this.run();

break;

}else {

this.setBlock(this.getUpRubPerson().getBlock());

printState(this.getPoint(), 1);

this.setRest(false);

this.getUpRubPerson().setRest(true);

}

}

try {

Thread.currentThread().sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

printState(this.getPoint(), 2);

this.nextRubPerson.run();

break;

case 2:

if(this.isRest()){

if(this.getUpRubPerson().isRest()){

this.run();

break;

}else {

this.setBlock(this.getUpRubPerson().getBlock());

printState(this.getPoint(), 1);

this.setRest(false);

this.getUpRubPerson().setRest(true);

}

}

try {

Thread.currentThread().sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

printState(this.getPoint(), 2);

this.setRest(true);

break;

default:

break;

}

}

private synchronized String printState(int role, int step) {

String result = "";

switch (role) {

case 0:

switch (step) {

case 1:

result = getName() + " 获得砖块.";

break;

case 2:

result = getName() + " 转递砖块给" + nextRubPerson.getName() + ".";

break;

default:

break;

}

break;

case 1:

switch (step) {

case 1:

result = this.getName() + " 得到砖块从" + upRubPerson.getName()

+ ".";

break;

case 2:

result = getName() + " 转递砖块给" + nextRubPerson.getName() + ".";

break;

default:

break;

}

break;

case 2:

switch (step) {

case 1:

result = getName() + " 得到砖块从" + upRubPerson.getName() + ".";

break;

case 2:

result = getName() + " 卸掉砖块.";

break;

default:

break;

}

break;

default:

break;

}

System.out.println(result);

return result;

}

public String getBlock() {

return block;

}

public synchronized void setBlock(String block) {

this.block = block;

}

public boolean isRest() {

return isRest;

}

public void setRest(boolean isRest) {

this.isRest = isRest;

}

public RubPerson getNextRubPerson() {

return nextRubPerson;

}

public void setNextRubPerson(RubPerson nextRubPerson) {

this.nextRubPerson = nextRubPerson;

}

public RubPerson getUpRubPerson() {

return upRubPerson;

}

public void setUpRubPerson(RubPerson upRubPerson) {

this.upRubPerson = upRubPerson;

}

public int getPoint() {

return point;

}

public void setPoint(int point) {

this.point = point;

}

}

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值