2014-02-22 19 views
-1

我有一個小的,但很奇怪的問題...
我需要從文件中讀取片段,並將它們放入數組中,這是讀出線程,但是當我想要他們從目前的線程,我得到空arrray。
我的大腦墜毀這些東西:卡在從另一個線程獲取數組

private int fragmentSize = 262144, fragmentCacheElements = 32, fragmentCacheUpdates = 0; 
// Cache 8Mb (265Kb*32)(262144*32) 
private String[] fragmentCache; 
private boolean needCacheUpdate, end; 
private Thread cacheThread = new Thread(new Runnable() 
{ 
    String[] fCache = new String[fragmentCacheElements]; 

    @Override 
    public void run() 
    { 
     while (!end) { 
      for (int i = 0; i < fragmentCacheElements; ++i) { 
       fCache[i] = new String(loadFragment(i + fragmentCacheUpdates * fragmentCacheElements)); 
      } 
      while (true) { 
       if (needCacheUpdate) { 
        ++fragmentCacheUpdates; 
        fragmentCache = fCache; 
        // fragment[0] != null 
        needCacheUpdate = false; 
        break; 
       } 
      } 
     } 
    } 
}); 

public static void main(String[] args) 
{ 
    fragmentCache = new String[fragmentCacheElements]; 
    cacheThread.start(); 
    updateCache(); 
    // Notifying client 
} 

// Getting fragment from cache to send it to client 
// AND WHY fragment[0] == null ??? 
private String getCache(int id) 
{ 
    if (id >= fragmentCacheUpdates * fragmentCacheElements) { 
     updateCache(); 
    } 
    return fragmentCache[id - (fragmentCacheUpdates - 1) * fragmentCacheElements]; 
} 

private void updateCache() 
{ 
    needCacheUpdate = true; 
    while (true) { 
     if (!needCacheUpdate) { 
      break; 
     } 
    } 
} 

有什麼建議?

+0

的值複製.. – nullpotent

+0

@iccthedral,你的意思是這樣做的循環? (int i = 0; i RussianVodka

回答

1

嘗試

fragmentCache = Arrays.copyOf(fCache, fCache.length); 
+0

啊..這是因爲我複製了鏈接到fCache,但沒有價值! 你天才,謝謝! – RussianVodka