News Uni Dev Res Blag

SPINNAN, THREADAN, SYNCAN

2010/10/24 - 19:14

/* Clusterfuck - shared resource chaos
 * Copyright (C) 2010  ed <irc.rizon.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, please refer to the following URL:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */


/* USER'S MANUAL
 * compile and hammer away
 */

Main.java

package derp;

public class Main {
    public static void main(String[] args) {
        Ringer ring = new Ringer(16);
        new Rander(ring).start();
        new Screener(ring).start();
        new Keyboarder(ring).start();
    }
}

Ringer.java

package derp;

public class Ringer {
    private int r, w;
    private String[] buffa;
    public Ringer(int size) {
        buffa = new String[size];
        for (int a = 0; a < size; a++)
            buffa[a] = null;
        r = w = 0;
    }
    public synchronized String read() {
        if (buffa[r] == null) {
            System.err.println("\rRINGBUFFER_EMPTY_EXCEPTION");
        } else if (r == w) {
            System.err.println("\rRINGBUFFER_FAILURE");
        } else {
            String ret = buffa[r];
            buffa[r] = null;
            r++;
            if (r >= buffa.length) r = 0;
            return ret;
        }
        return null;
    }
    public synchronized void write(String str) {
        if (buffa[w] != null) {
            System.err.println("\rRINGBUFFER_FULL_EXCEPTION");
        } else {
            buffa[w] = str;
            w++;
            if (w >= buffa.length) w = 0;
        }
    }
}

Screener.java

package derp;

public class Screener extends Thread {
    final int DELAY = 1000;
    private Ringer ring;
    public Screener(Ringer ring) {
        this.ring = ring;
    }
    public void run() {
        while (true) {
            String str = ring.read();
            if (str != null)
                System.out.println("\r" + str);
            
            try {
                Thread.sleep(DELAY);
            }catch(Exception ex){
            }
        }
    }
}

Keyboarder.java

package derp;
import java.util.Scanner;

public class Keyboarder extends Thread {
    private Ringer ring;
    private Scanner kb;
    public Keyboarder(Ringer ring) {
        this.ring = ring;
        kb = new Scanner(System.in);
    }
    public void run() {
        while (true) {
            ring.write(kb.nextLine());
        }
    }
}

Rander.java

package derp;

public class Rander extends Thread {
    final int DELAY = 900;
    final int LEN = 4;

    Ringer ring;
    public Rander(Ringer ring) {
        this.ring = ring;
    }
    public void run() {
        while (true) {
            StringBuilder sb = new StringBuilder();
            for (int a = 0; a < LEN; a++) {
                sb.append((char)(Math.random()*26+'a'));
            }
            ring.write(sb.toString());
            try {
                Thread.sleep(DELAY);
            }catch(Exception ex) {
            }
        }
    }
}

Copyright © 2010 Ed Edland