---
title: "多线程并发思考--文件加锁"
author: "cjc"
date: "2006-12-12T22:17:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2006-12-12T22:17:00Z"
id: 447008
url: "https://cjc.iteye.com/blog/447008"
---

### 多线程并发思考--文件加锁

在最近的工作中,经常要用到线程,就对线程相关知识稍微看了看,知道并发线程经常引起共享资源冲突,java以提供关键字synchronized的形式,为防止资源冲突提供了内置支持.

可是在工作中,我却碰到了这样的需求,定时抛出线程读写某文件的内容,由于相隔时间很短,我突然想到,会不会在第二次轮循开始对该文件进行读操作的时候,第一次抛出的线程还在对该文件进行写操作,如果有可能,那么第二次读出的数据会是什么样的呢?

怀着这样的疑问,我开始以程序作实验,代码如下:

> 1.用于写文件的线程
> 
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packagechb.thread;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.BufferedWriter;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.File;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.FileWriter;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.IOException;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.Calendar;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@author崔红保  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*这个线程用于写文件  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassThread\_writeFileextendsThread...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidrun()...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Calendarcalstart=Calendar.getInstance();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Filefile=newFile("D:/test.txt");  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(!file.exists())  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)file.createNewFile();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)FileWriterfw=newFileWriter(file);  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)BufferedWriterbw=newBufferedWriter(fw);  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(inti=0;i&lt;1000;i++)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sleep(10);  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)bw.write("这是第"+(i+1)+"行，应该没错哈 ");  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)bw.close();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)bw=null;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fw.close();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fw=null;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(IOExceptione)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(InterruptedExceptione)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Calendarcalend=Calendar.getInstance();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.out.println("写文件共用了"+(calend.getTimeInMillis()-calstart.getTimeInMillis())+"毫秒");  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
> 
> 2.用于读文件的线程
> 
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packagechb.thread;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.BufferedReader;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.File;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.FileNotFoundException;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.FileReader;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.IOException;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.Calendar;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@author崔红保  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*这个线程用于读文件  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassThread\_readFileextendsThread...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidrun()...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Calendarcalstart=Calendar.getInstance();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sleep(5000);  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Filefile=newFile("D:/test.txt");  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)BufferedReaderbr=newBufferedReader(newFileReader(file));  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringtemp=null;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)temp=br.readLine();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while(temp!=null)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.out.println(temp);  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)temp=br.readLine();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)br.close();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)br=null;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Calendarcalend=Calendar.getInstance();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.out.println("读文件共用了"+(calend.getTimeInMillis()-calstart.getTimeInMillis())+"毫秒");  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(FileNotFoundExceptione)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(IOExceptione)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(InterruptedExceptione)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
> 
> 3.分别启用两个线程
> 
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thread\_writeFilethf3=newThread\_writeFile();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thread\_readFilethf4=newThread\_readFile();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)thf3.start();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)thf4.start();
> 
> 4.结果分析
> 
> 虽然写文件的操作开始5秒钟后,读文件的操作才开始进行,可是读文件的线程并没有读出数据,改变时间,读出的数据也就各不相同.

为了避免以上结果,我们希望在一个线程在操作某个文件的时候,其他线程不能对该文件进行读或写操作,要怎么才能实现呢?利用java提供的synchronized似乎无法完成,因为每个线程是在程序中动态抛出的.郁昧了一天之后,我终于找到了一个解决办法,就是利用java.nio包中的FileChannel对文件进行加锁.

具体实现方法如下:

> 1.写文件的线程
> 
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packagechb.thread;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.File;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.FileOutputStream;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.IOException;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.nio.channels.FileChannel;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.nio.channels.FileLock;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.Calendar;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchb  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassThread\_writeFileextendsThread...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidrun()...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Calendarcalstart=Calendar.getInstance();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Filefile=newFile("D:/test.txt");  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(!file.exists())  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)file.createNewFile();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//对该文件加锁  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)FileOutputStreamout=newFileOutputStream(file,true);  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)FileChannelfcout=out.getChannel();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)FileLockflout=null;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while(true)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)flout=fcout.tryLock();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(flout!=null)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)else...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.out.println("有其他线程正在操作该文件，当前线程休眠1000毫秒");  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sleep(100);  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(inti=1;i&lt;=1000;i++)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sleep(10);  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)StringBuffersb=newStringBuffer();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sb.append("这是第"+i+"行，应该没啥错哈 ");  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)out.write(sb.toString().getBytes("utf-8"));  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)flout.release();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fcout.close();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)out.close();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)out=null;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(IOExceptione)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(InterruptedExceptione)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Calendarcalend=Calendar.getInstance();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.out.println("写文件共花了"+(calend.getTimeInMillis()-calstart.getTimeInMillis())+"秒");  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
> 
> 2.读文件的线程
> 
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packagechb.thread;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.File;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.FileInputStream;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.FileNotFoundException;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.IOException;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.nio.channels.FileChannel;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.nio.channels.FileLock;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.Calendar;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchb  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*���ļ�  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassThread\_readFileextendsThread...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidrun()...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Calendarcalstart=Calendar.getInstance();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sleep(5000);  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Filefile=newFile("D:/test.txt");  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//给该文件加锁  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)FileInputStreamfis=newFileInputStream(file);  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)FileChannelfcin=fis.getChannel();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)FileLockflin=null;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while(true)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)flin=fcin.tryLock(0,Long.MAX\_VALUE,true);  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(flin!=null)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)else...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.out.println("有其他线程正在操作该文件，当前线程休眠1000毫秒");  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sleep(1000);  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)byte\[]buf=newbyte\[1024];  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)StringBuffersb=newStringBuffer();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while((fis.read(buf))!=-1)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sb.append(newString(buf,"utf-8"));  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)buf=newbyte\[1024];  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.out.println(sb.toString());  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)flin.release();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fcin.close();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fis.close();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fis=null;  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Calendarcalend=Calendar.getInstance();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.out.println("读文件共花了"+(calend.getTimeInMillis()-calstart.getTimeInMillis())+"秒");  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(FileNotFoundExceptione)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(IOExceptione)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(InterruptedExceptione)...{  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
> 
> 3.分别启用两个线程
> 
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thread\_writeFilethf3=newThread\_writeFile();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thread\_readFilethf4=newThread\_readFile();  
> ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)th