Java codepublic class MyThread extends Thread{ private CountDownLatch threadsSignal; private int hsmapCapacity; private long forAndVal; private boolean DNAFormat; private long forAndVal32; public MyThread(CountDownLatch threadsSignal, int capacity, boolean DNAFormat){ super(); this.threadsSignal = threadsSignal; this.hsmapCapacity = capacity; this.forAndVal = (long)Math.pow(2, 2*(k-32)) - 1; this.DNAFormat = DNAFormat; this.forAndVal32 = (long)Math.pow(2, 2*k) - 1; } @Override public void run(){ System.out.println(Thread.currentThread().getName() + "Start..."); FileReader fr; BufferedReader bfr; FileWriter fw = null; BufferedWriter bfw = null; DataOutputStream out = null; HashMap nodes = new HashMap(hsmapCapacity); String line; Random rd = new Random(); int p,j; Kmer k1, k1_rev; try{ File dir = new File("Maps(count)"); if(!dir.exists()) dir.mkdir(); nodes.clear(); while(blocks.cardinality()> iter = nodes.entrySet().iterator(); while (iter.hasNext()) { Entry entry = (Entry) iter.next(); if(!DNAFormat){ if(k > 32) out.writeLong(entry.getKey().high); out.writeLong(entry.getKey().low); out.writeInt(entry.getValue()); } else{ bfw.write(entry.getKey().toDNA(k)+"\t"+entry.getValue()+"\n"); } synchronized(lock){ stat[entry.getValue()]++; } } synchronized(lock_2){ totalNumNodes+=nodes.size(); } nodes.clear(); if(DNAFormat) { bfw.close(); fw.close(); } else out.close(); bfr.close(); fr.close(); } }catch(Exception E){ System.out.println("Exception caught!"); E.printStackTrace(); } threadsSignal.countDown(); System.out.println(Thread.currentThread().getName() + "End. Remaining" + threadsSignal.getCount() + " threads"); } } private void BuildMap(int threadNum, int hsmapCapacity, boolean DNAFormat) throws Exception{ CountDownLatch threadSignal = new CountDownLatch(threadNum); for(int i=0;i
java多线程为什么加锁,问个java多线程加锁的有关问题
最新推荐文章于 2024-08-08 17:21:25 发布