`
收藏列表
标题 标签 来源
多进程读写文件:一个进程A写文件file,另一个进程B读文件file 算法与数据结构
多进程读写文件:一个进程A写文件file,另一个进程B读文件file
Doug Lea 在他的书中提供一个示例代码
ReadWrite 为抽象类,允许并发的读操作,不允许并发的写操作,也不允许读写同时进行。
可以扩展ReadWrite为SingleFileReadWrite 实现 doRead和doWrite方法来读写文件
线程A和线程B使用同一个SingleFileReadWrite 实例
SingleFileReadWrite rw;
threadB rw.read();
threadA rw.write();

Java代码

    abstract class ReadWrite {  
     protected int activeReaders = 0;  // threads executing read  
     protected int activeWriters = 0;  // always zero or one  
      
     protected int waitingReaders = 0; // threads not yet in read  
     protected int waitingWriters = 0; // same for write  
      
     protected abstract void doRead(); // implement in subclasses  
     protected abstract void doWrite();  
      
     public void read() throws InterruptedException {  
      beforeRead();  
      try   { doRead(); }  
      finally { afterRead(); }  
     }  
      
     public void write() throws InterruptedException {  
      beforeWrite();  
      try     { doWrite(); }  
      finally { afterWrite(); }  
     }  
     protected boolean allowReader() {  
      return waitingWriters == 0 && activeWriters == 0;  
     }  
      
     protected boolean allowWriter() {  
      return activeReaders == 0 && activeWriters == 0;  
     }  
      
     protected synchronized void beforeRead()  
      throws InterruptedException {  
       ++waitingReaders;  
       while (!allowReader()) {  
        try { wait(); }  
        catch (InterruptedException ie) {  
          --waitingReaders; // roll back state  
          throw ie;  
        }  
      }  
      --waitingReaders;  
      ++activeReaders;  
     }  
      
     protected synchronized void afterRead()  {  
      --activeReaders;  
      notifyAll();  
     }  
      
     protected synchronized void beforeWrite()  
      throws InterruptedException {  
       ++waitingWriters;  
       while (!allowWriter()) {  
        try { wait(); }  
        catch (InterruptedException ie) {  
         --waitingWriters;  
         throw ie;  
        }  
       }  
       --waitingWriters;  
       ++activeWriters;  
     }  
      
     protected synchronized void afterWrite() {  
      --activeWriters;  
      notifyAll();  
      }  
    }  
微软算法面试题目 算法与数据结构 http://blog.csdn.net/jernymy/article/details/6639534
    <pre name="code" class="cpp">/******************************************************************************* 
    文 件 名 : test100.cpp 
    实现功能 : 微软面试题: 
               有100万个数字(1到9),其中只有1个数字重复2次,如何快速找出该数字 
               补充下题目 意思:我这里每个数字 都是个阿拉伯数字,是1位的,是1到9之间的 
               但是有1百万个这样的数字 比如 12345678912 是11个这样的数字 
    实现思路 : 定义9个数组,记录1-9的出现的数字做标记,出现一次则,对应数组加1, 
               超过2次,在下次循环直接结束,这样一共直到记录8个数字后, 
               直接打印第9个数字,就是一共只会出现2次的数字了 
    作    者 : jernymy 
    版    权 : <Copyright(C) ... jernymy.> 
    日    期 : 2011-07-28 
    版    本 : V1.1 
    *******************************************************************************/  
      
    #include "stdio.h"  
    #include "stdlib.h"  // srand  
    #include "time.h"    // time  
    #include "string.h"  // memset  
      
    // 最大的数据是100万, 1000000  
    #define MAX_NUM  1000*1000  
      
    // 出现频率的个数  
    #define OVER_VAR  2  
      
    // 记录1-9这几个数字出现的次数,是否出现超过2次,  
    // 如果超过2次可以不用判断,初始化为0  
    unsigned char g_abyNumAry[10];  
      
    // 记录总共出现超过3次的数字的个数,当超过7个时,即为8时。  
    // 则可以直接打印出没有出现超过2次的那个数字,就一定是公出现2次的数字了。  
    unsigned char g_nCount = 0;  
      
    int main(void)  
    {  
        int nIdx;  
        int nData;  
        time_t nCurTime;   
      
        srand(time(&nCurTime));  
        memset(g_abyNumAry, 0, sizeof(g_abyNumAry));  
      
        for (nIdx = 0; nIdx < MAX_NUM; nIdx++)  
        {  
            nData = rand()%9 + 1; // 随机数从1到9,测试专用,jernymy from 1-9  
      
            printf("%d ", nData); // 打印该随机数字  
              
            // 此数字出现次数大于2次,则退出次循环  
            if (g_abyNumAry[nData] > OVER_VAR)  
            {  
                continue;  
            }  
      
            // 此数字出现1次,在大于2次时,就对总个数加1  
            if (++g_abyNumAry[nData] > OVER_VAR)  
            {  
                g_nCount++;  
            }  
          
            // 当有8个数字出现的个数均超过2次,  
            // 那么剩下的那个数字一定就是要求的了  
            if (g_nCount == 8)  
            {  
                printf("\nfound only 2 num: ");  
                for (nIdx = 1; nIdx < 10; nIdx++)  
                {  
                    if (OVER_VAR == g_abyNumAry[nIdx])  
                    {  
                        // jernymy 此数就是我们要找的那个了  
                        printf("%d\n", nIdx);  
                        break;  
                    }  
                }  
                break;  
            }      
        }  
      
        // jernymy-20110728 当没有8个数字出现的个数均超过2次,  
        // 那么求出仅出现2次的那个数字即可  
        if (g_nCount < 8)  
        {  
            printf("\nfound only 2 num: ");  
            for (nIdx = 1; nIdx < 10; nIdx++)  
            {  
                if (OVER_VAR == g_abyNumAry[nIdx])  
                {  
                    // jernymy 此数就是我们要找的那个了  
                    printf("%d\n", nIdx);  
                    break;  
                }  
            }  
        }  
        // jernymy-20110728  
      
        return 0;  
    }  
Global site tag (gtag.js) - Google Analytics