Executor Service执行只是运行第一个线程

时间:2017-01-01 23:57:18

标签: java multithreading concurrency threadpool executorservice

我正在用Java编写一个模拟令牌环算法的程序。 我有许多Jugador对象竞争共享资源Balon。 当我想执行使用ExecutorService创建的多个线程时,似乎只有第一个线程正在运行,并且由于 run方法被覆盖是一个infinte循环,程序将永远保留在该循环中。

在控制台中,我只执行第一个循环,然后程序就会保持在无限循环中。

Jugador班:

public class Jugador implements Runnable{
    private Boolean testimonio;
    private Balon balon;
    Scanner sc = new Scanner(System.in);

    public void darPataditas(){
        this.balon.aumentarPataditas();        
    }

    public void recibirTestimonio(){
        System.out.println(this.getNombre() + ", deseas recibir el testimonio (1/0)");
        int rpta = sc.nextInt();
        if(rpta == 1)   this.testimonio = true;
        else
            this.pasarTestimonio();
    }

    public void  pasarTestimonio(){
        this.testimonio = false;
        System.out.println("Yo, "+this.getNombre()+ ", estoy pasando el testimonio a "+ this.getSiguiente().getNombre());
        this.siguiente.recibirTestimonio();
    }

    @Override
    public void run(){
        try{
            while(true){
                if(this.testimonio && this.balon.tomarBalon()){
                    this.darPataditas();
                    this.balon.dejarBalon();
                    this.pasarTestimonio();
                    System.out.print(this.getNombre() + ", ¿seguiras jugando?(1/0)");
                    if(sc.nextInt() == 0){
                        this.anterior.setSiguiente(this.siguiente);
                        break;
                    }
                }
            }   
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}

班巴隆:

public class Balon {
    private int pataditas;
    Lock up = new ReentrantLock();

    public Balon() {
        this.pataditas = 0;
    }

    public void aumentarPataditas(){
        pataditas++;
    }

    public boolean tomarBalon(){
        if(up.tryLock()) return true;
        else return false;
    }

    public void dejarBalon(){
        up.unlock();
    }
}

主类:

public class TokenRing {

    private static final int NUM_JUGADORES = 5;
    public static void main(String[] args) throws InterruptedException{        
        Scanner sc = new Scanner(System.in);

        Jugador[] jugadores = null;
        Balon pelota = new Balon();
        jugadores = new Jugador[NUM_JUGADORES];
        for(int i = 0; i < NUM_JUGADORES; i++){
            System.out.print("Ingrese nombre del jugador: ");
            String name = sc.next();
            jugadores[i] = new Jugador(name, i+1, pelota);
        }

           jugadores[0].setTestimonio(true);
        for(int i = 0; i < NUM_JUGADORES-1; i++){
            jugadores[i].setSiguiente(jugadores[i+1]);
            jugadores[i+1].setAnterior(jugadores[i]);
        }
        jugadores[NUM_JUGADORES-1].setSiguiente(jugadores[0]);
        jugadores[0].setAnterior(jugadores[NUM_JUGADORES-1]);

        ExecutorService threadPool = Executors.newFixedThreadPool(NUM_JUGADORES);
        try{
            for(int i = 0; i < NUM_JUGADORES; i++){
                threadPool.execute(jugadores[i]);
            }
            Thread.sleep(10000);
        }finally{
            threadPool.shutdown();

            while (!threadPool.isTerminated()) {
                Thread.sleep(1000);
            }
            System.out.println("El balon dio " + pelota.getPataditas() + " pataditas.");
        }

    }
}

您可以查看我的Github Repository完整代码 非常感谢。

0 个答案:

没有答案
相关问题