-2

ループがありますが、重すぎるので複数のスレッドで同じものを共有したいのですが、その方法がわかりません。

while(!labyrinth.exit(bob3) && !labyrinth.exit(bob2)) {

     Collection<Room> accessibleRooms = labyrinth.accessibleRooms(bob3);

     if (bob3.canMove()) {
         destination = bob3.makeChoice(accessibleRooms);
     }

     if (destination != bob3.getPosition()) {
         destination.receive(bob3);
     }

     accessibleRooms = labyrinth.accessibleRooms(bob2);   

     if (bob2.canMove()) {
         destination = bob2.makeChoice(accessibleRooms);
     }

     if (destination != bob2.getPosition()) {
         destination.receive(bob2);
     }
}

ご覧のとおり、このループには 2 つの同一の操作があるため、それぞれに異なるスレッドを使用させることができます。

4

2 に答える 2

4

「マルチスレッド」を作成する最も簡単な方法:

while(....){
    new Thread(new Runnable(){
            public void run(){
               // put your code here.
            }
        }
    ).start();
}

変数を final にすることを忘れないでください

編集:あなたはそれを望んでいました:)

ExecutorService exec= Executors.newCachedThreadPool()
while(....){
    exec.execute(new Runnable(){
            public void run(){
               // put your code here.
            }
        }
    );
}
于 2012-12-19T09:42:10.203 に答える
1
    final CyclicBarrier c = new CyclicBarrier(2);

    Runnable r1 = new Runnable()
    {

        @Override
        public void run()
        {
            while ( !labyrinthe.sortir(bob3) )
            {

                Collection<Salle> sallesAccessibles = labyrinthe.sallesAccessibles(bob3);
                if ( bob3.peutSeDeplacer() )
                    destination = bob3.faitSonChoix(sallesAccessibles); // on demande au heros de faire son choix de salle
                if ( destination != bob3.getPosition() )
                    destination.recevoir(bob3); // deplacement
            }
            try
            {
                c.await();
            }
            catch ( InterruptedException e )
            {
                ;
            }
            catch ( BrokenBarrierException e )
            {
                ;
            }
        }
    };

    Runnable r2 = new Runnable()
    {
        @Override
        public void run()
        {
            while ( !labyrinthe.sortir(bob2) )
            {
                Collection<Salle> sallesAccessibles = labyrinthe.sallesAccessibles(bob2);
                if ( bob2.peutSeDeplacer() )
                    destination = bob2.faitSonChoix(sallesAccessibles); // on demande au heros de faire son choix de salle
                if ( destination != bob2.getPosition() )
                    destination.recevoir(bob2); // deplacement

            }
            try
            {
                c.await();
            }
            catch ( InterruptedException e )
            {
                ;
            }
            catch ( BrokenBarrierException e )
            {
                ;
            }
        }
    };

    Thread t1 = new Thread(r1);
    Thread t2 = new Thread(r2);

    t1.start();
    t2.start();

    c.await();

    System.out.println("Done");
于 2012-12-19T09:48:10.053 に答える