主题:【原创】将24进行到底 -- 泰让
这里是多线程部分。用了一些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);
}
}
- 相关回复 上下关系8
😁今天晚上我把souce code给你打包发过去。 1 Highway 字42 2007-03-09 20:15:28
😄发我一份呀 泰让 字30 2007-03-09 21:45:43
🙂哈,你们论剑的时候别忘了俺! 面壁 字91 2007-03-10 11:38:58
😄那就贻笑大方了。
🙂【注释之1】总体原理 3 泰让 字1624 2007-03-06 17:38:21
🙂【注释之2】程序实现 3 泰让 字1086 2007-03-06 21:40:27
🙂还是烦请老兄再添点儿注解。。。 面壁 字151 2007-03-06 16:27:36
😄谢谢老弟的捧场。 1 Highway 字281 2007-03-06 08:42:13