java多线程为什么加锁,问个java多线程加锁的有关问题

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值