西西河

主题:【原创】将24进行到底 -- 泰让

共:💬28 🌺15
全看分页树展 · 主题 跟帖
家园 那就贻笑大方了。

这里是多线程部分。用了一些JDK 5.0以后的new feature,所以可能看起来有些“异怪”。加了很简单一些注释,希望能有点小小帮助。

package game;

import java.util.Map;

import java.util.Set;

import java.util.concurrent.ConcurrentHashMap;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

/**

* This one has ability to scale up to multiple CPUs. It is

* subclass of plain version of PokerGame

*/

public class PokerGameMT extends PokerGame

{

//uses the latest Java concurrent feature--Thread pool

private final ExecutorService pool =

Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

private final Object OBJ = new Object();

/**

* Inner class, do real calculation job. Basically it is just another

* way to execute calculate() function defined in base class

*/

private class Runner implements Runnable

{

private int[] set;

private Map<String, Object> solutions;

private CountDownLatch latch;

Runner(int[] set, Map<String, Object> solutions, CountDownLatch latch)

{

this.set = set;

this.solutions = solutions;

this.latch = latch;

}

public void run()

{

for (Operator[] opCombo : opCombos)

{

Set<int[]> calcSeq = PokerGameMT.this.getCalcSequence(opCombo);

for (int[] seq : calcSeq)

{

String solution = PokerGameMT.this.calculate(set, opCombo, seq);

if (solution != null)

{

solutions.put(solution, OBJ);

}

}

}

latch.countDown();

}

}

/**

* Given a number of poker, find the total sulotions

*/

protected Set<String> findSolution(int[] nums)

{

/**

* use ConcurrentHashMap is for performance reason. Otherwise,

* Set<String> would be just fine.

*/

Map<String, Object> solutions = new ConcurrentHashMap<String, Object>();

Map<Integer, int[]> workingSet = new ConcurrentHashMap<Integer, int[]>();

int[] aPokeSet = new int[nums.length];

for(int[] set : this.permutations)

{

for (int i = 0; i < nums.length; i++)

{

aPokeSet[i] = nums[set[i]];

}

/**

* avoid duplication calculation. Only different set

* is sent to runner threads.

*/

int code = this.getPokeHashCode(aPokeSet);

if (!workingSet.containsKey(code))

{

int[] aCalcset = new int[nums.length];

System.arraycopy(aPokeSet, 0, aCalcset, 0, nums.length);

workingSet.put(code, aCalcset);

}

}

int totalPerm = workingSet.size();

//new feature of JDK 5.0 or after. Just like old join()

CountDownLatch latch = new CountDownLatch(totalPerm);

//submit job to runner thread

for(int[] set : workingSet.values())

{

pool.execute(new Runner(set, solutions, latch));

}

try

{

//wait for runner finish calculation

latch.await();

}catch(InterruptedException ex)

{

ex.printStackTrace();

}

return solutions.keySet();

}

/**

* tear down the thread pool

*/

protected void close()

{

super.close();

pool.shutdownNow();

}

/**

* Constructor. first one specify poke number and

* second one specify expected value, 24 for instance

* @param pokeNum

* @param answer

*/

public PokerGameMT(int pokeNum, int answer)

{

super(pokeNum, answer);

}

}

全看分页树展 · 主题 跟帖


有趣有益,互惠互利;开阔视野,博采众长。
虚拟的网络,真实的人。天南地北客,相逢皆朋友

Copyright © cchere 西西河