    001    package net.minecraft.src;
    002
    003    import java.util.ArrayList;
    004    import java.util.Collections;
    005    import java.util.List;
    006
    007    public class ThreadedFileIOBase implements Runnable
    008    {
    009        /** Instance of ThreadedFileIOBase */
    010        public static final ThreadedFileIOBase threadedIOInstance = new ThreadedFileIOBase();
    011        private List threadedIOQueue = Collections.synchronizedList(new ArrayList());
    012        private volatile long writeQueuedCounter = 0L;
    013        private volatile long savedIOCounter = 0L;
    014        private volatile boolean isThreadWaiting = false;
    015
    016        private ThreadedFileIOBase()
    017        {
    018            Thread var1 = new Thread(this, "File IO Thread");
    019            var1.setPriority(1);
    020            var1.start();
    021        }
    022
    023        public void run()
    024        {
    025            while (true)
    026            {
    027                this.processQueue();
    028            }
    029        }
    030
    031        /**
    032         * Process the items that are in the queue
    033         */
    034        private void processQueue()
    035        {
    036            for (int var1 = 0; var1 < this.threadedIOQueue.size(); ++var1)
    037            {
    038                IThreadedFileIO var2 = (IThreadedFileIO)this.threadedIOQueue.get(var1);
    039                boolean var3 = var2.writeNextIO();
    040
    041                if (!var3)
    042                {
    043                    this.threadedIOQueue.remove(var1--);
    044                    ++this.savedIOCounter;
    045                }
    046
    047                try
    048                {
    049                    Thread.sleep(this.isThreadWaiting ? 0L : 10L);
    050                }
    051                catch (InterruptedException var6)
    052                {
    053                    var6.printStackTrace();
    054                }
    055            }
    056
    057            if (this.threadedIOQueue.isEmpty())
    058            {
    059                try
    060                {
    061                    Thread.sleep(25L);
    062                }
    063                catch (InterruptedException var5)
    064                {
    065                    var5.printStackTrace();
    066                }
    067            }
    068        }
    069
    070        /**
    071         * threaded io
    072         */
    073        public void queueIO(IThreadedFileIO par1IThreadedFileIO)
    074        {
    075            if (!this.threadedIOQueue.contains(par1IThreadedFileIO))
    076            {
    077                ++this.writeQueuedCounter;
    078                this.threadedIOQueue.add(par1IThreadedFileIO);
    079            }
    080        }
    081
    082        public void waitForFinish() throws InterruptedException
    083        {
    084            this.isThreadWaiting = true;
    085
    086            while (this.writeQueuedCounter != this.savedIOCounter)
    087            {
    088                Thread.sleep(10L);
    089            }
    090
    091            this.isThreadWaiting = false;
    092        }
    093    }