All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codingame.gameengine.runner.AsynchronousWriter Maven / Gradle / Ivy

Go to download

Runner / Testbed to locally run Games created using the CodinGame engine toolkit.

There is a newer version: 4.5.0
Show newest version
package com.codingame.gameengine.runner;

import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.BlockingQueue;

class AsynchronousWriter extends Thread {

    private final BlockingQueue queue;
    private final OutputStream stream;
    private boolean interrupt;

    public AsynchronousWriter(BlockingQueue queue, OutputStream stream) {
        this.queue = queue;
        this.stream = stream;
        this.interrupt = false;
    }

    @Override
    public void run() {
        while (!interrupt) {
            try {
                String toWrite = queue.take();
                if (this.stream == null || toWrite == null || GameRunner.INTERRUPT_THREAD.equals(toWrite)) {
                    interrupt = true;
                } else {
                    stream.write(toWrite.getBytes(Agent.UTF8));
                    stream.flush();
                }
            } catch (InterruptedException | IOException e) {
                interrupt = true;
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy