| 1 | package net.minecraft.world.storage;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.Collections;
|
| 5 | import java.util.List;
|
| 6 |
|
| 7 | public class ThreadedFileIOBase implements Runnable
|
| 8 | {
|
| 9 | /** Instance of ThreadedFileIOBase */
|
| 10 | private static final ThreadedFileIOBase threadedIOInstance = new ThreadedFileIOBase();
|
| 11 | private List<IThreadedFileIO> threadedIOQueue = Collections.<IThreadedFileIO>synchronizedList(Lists.<IThreadedFileIO>newArrayList());
|
| 12 | private volatile long writeQueuedCounter;
|
| 13 | private volatile long savedIOCounter;
|
| 14 | private volatile boolean isThreadWaiting;
|
| 15 |
|
| 16 | private ThreadedFileIOBase()
|
| 17 | {
|
| 18 | Thread thread = new Thread(this, "File IO Thread");
|
| 19 | thread.setPriority(1);
|
| 20 | thread.start();
|
| 21 | }
|
| 22 |
|
| 23 | /**
|
| 24 | * Retrieves an instance of the threadedFileIOBase.
|
| 25 | */
|
| 26 | public static ThreadedFileIOBase getThreadedIOInstance()
|
| 27 | {
|
| 28 | return threadedIOInstance;
|
| 29 | }
|
| 30 |
|
| 31 | public void run()
|
| 32 | {
|
| 33 | while (true)
|
| 34 | {
|
| 35 | this.processQueue();
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | /**
|
| 40 | * Process the items that are in the queue
|
| 41 | */
|
| 42 | private void processQueue()
|
| 43 | {
|
| 44 | for (int i = 0; i < this.threadedIOQueue.size(); ++i)
|
| 45 | {
|
| 46 | IThreadedFileIO ithreadedfileio = (IThreadedFileIO)this.threadedIOQueue.get(i);
|
| 47 | boolean flag = ithreadedfileio.writeNextIO();
|
| 48 |
|
| 49 | if (!flag)
|
| 50 | {
|
| 51 | this.threadedIOQueue.remove(i--);
|
| 52 | ++this.savedIOCounter;
|
| 53 | }
|
| 54 |
|
| 55 | try
|
| 56 | {
|
| 57 | Thread.sleep(this.isThreadWaiting ? 0L : 10L);
|
| 58 | }
|
| 59 | catch (InterruptedException interruptedexception1)
|
| 60 | {
|
| 61 | interruptedexception1.printStackTrace();
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | if (this.threadedIOQueue.isEmpty())
|
| 66 | {
|
| 67 | try
|
| 68 | {
|
| 69 | Thread.sleep(25L);
|
| 70 | }
|
| 71 | catch (InterruptedException interruptedexception)
|
| 72 | {
|
| 73 | interruptedexception.printStackTrace();
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | /**
|
| 79 | * threaded io
|
| 80 | */
|
| 81 | public void queueIO(IThreadedFileIO p_75735_1_)
|
| 82 | {
|
| 83 | if (!this.threadedIOQueue.contains(p_75735_1_))
|
| 84 | {
|
| 85 | ++this.writeQueuedCounter;
|
| 86 | this.threadedIOQueue.add(p_75735_1_);
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | public void waitForFinish() throws InterruptedException
|
| 91 | {
|
| 92 | this.isThreadWaiting = true;
|
| 93 |
|
| 94 | while (this.writeQueuedCounter != this.savedIOCounter)
|
| 95 | {
|
| 96 | Thread.sleep(10L);
|
| 97 | }
|
| 98 |
|
| 99 | this.isThreadWaiting = false;
|
| 100 | }
|
| 101 | }
|
| 102 |
|