日期:2014-05-20  浏览次数:20734 次

两个线程打台球的问题 求解决
问题是这样:
1、创建两个线程
2、1-15正整数放入集合中(map,list)均可
3、分配两个线程取值范围(1-7/9-15)
4、两个线程按照打台球规则依次在集合中取值(最后取8)
5、判断是否某个线程取值结束
6、每次需要输出取值的状态


能否说明一下思路 谢谢~

------解决方案--------------------
Java code
import java.util.Vector;

public class TestThread {
    public static void main(String[] args) {
        Vector<String> avector = new Vector<String>();
        avector.add("1");
        avector.add("2");
        avector.add("3");
        avector.add("4");
        avector.add("5");
        avector.add("6");
        avector.add("7");
        Vector<String> bvector = new Vector<String>();
        bvector.add("9");
        bvector.add("10");
        bvector.add("11");
        bvector.add("12");
        bvector.add("13");
        bvector.add("14");
        bvector.add("15");
        ThreadMain bThreadMain =new ThreadMain(avector,bvector);
        Thread thread = new Thread(bThreadMain);
        
        thread.start();

    }
    
}

//线程
import java.util.Random;
import java.util.Vector;

public class ThreadMain implements Runnable {
    //小号
    private Vector<String> avector;
    //大号
    private Vector<String> bvector;
    // 取什么号 a小号 b大号
    private String num = "a";
    //正在取球的
    private Vector<String> vector;

    public void run() {
        boolean flag = true;
        while (flag) {
            if(null==vector){
                //随机选择谁先开始
                if(whoStart()){
                    vector = avector;num = "a";
                }else{
                    vector = bvector;num = "b";
                }
            }
            //开始击球
            if (isSuccess()) {
                // 成功击球
                if(vector.size()==0){
                 //已有人将球击完 击黑8
                    if(isSuccess()){
                        System.out.println(num+"击球黑8成功 取得胜利");
                        flag = false;
                    }else{
                        // 换人击球
                        changeMan();
                    }
                }else{
                    int qiu = new Random().nextInt(vector.size());
                    System.out.println(num+"击球"+vector.get(qiu)+"成功");
                    vector.remove(qiu);
                }
            } else {
                // 换人击球
                changeMan();
            }
        }
    }

    public  void changeMan(){
        // 换人击球
        if(num.equals("a")){
            vector = bvector;num = "b";
        }else{
            vector = avector;num = "a";
        }
    }
    
    
    /**
     * 是否成功击球入袋 每次都有50%成功机会
     */
    public static boolean isSuccess() {
        int i = new Random().nextInt(2);
        if (i == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 谁先
     */
    public static boolean whoStart() {
        int i = new Random().nextInt(2);
        if (i == 1) {
            return true;
        } else {
            return false;
        }
    }
    public ThreadMain(Vector<String> avector, Vector<String> bvector) {
        this.avector = avector;
        this.bvector = bvector;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

}

------解决方案--------------------
Java code

import java.util.concurrent.ArrayBlockingQueue;

public class TableBall
{
    private static String winner = null;

    class Player extends Thread
    {
        private ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<Integer>(
                8);

        private String name;

        private int ball;

        private boolean score = true;

        public void setScore(boolean score)
        {
            this.score = score;
        }

        private double scoreRate;

        public boolean initBall(int[] balls)
        {
            for (Integer ball : balls)
            {
                if (ball > 0 && ball < 16)
                {
                    try
                    {
                        abq.put(ball);
                    }
                    catch (InterruptedException e)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        Player(String name, double scoreRate)
        {
            this.name = name;
            this.scoreRate = scoreRate;
        }

        @Override
        public void run()
        {
            // 上局是否打进
            while (score)
            {
                // 取下个球
                try
                {
                    ball = abq.take();
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }

                double rate = Math.random();
                if (rate <= scoreRate)
                {
                    score = true;
                    System.out.println("Player " + name + " kick in ball "
                            + ball);
                }
                else
                {
                    score = false;
                    System.out.println("Player " + name + " miss ball " + ball);
                }
                if (score && abq.size() == 0 && winner == null)
                {
                    winner = name;
                    System.out.println(name + " wins!");
                    break;
                }
            }
        }
    }

    public static void main(String[] args)
    {
        TableBall tb = new TableBall();
        Player p1 = tb.new Player("uery", 0.5);
        p1.initBall(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 });
        Player p2 = tb.new Player("yan", 1);
        p2.initBall(new int[] { 9, 10, 11, 12, 13, 14, 15, 8 });
        while (winner == null)
        {
            p1.setScore(true);
            p1.run();
            if (winner == null)
            {
                p2.setScore(true);
                p2.run();
            }
            try
            {
                Thread.sleep(500);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }

}