Last active
March 8, 2016 20:30
-
-
Save boxmein/84c6198c8743ddefcd9a to your computer and use it in GitHub Desktop.
janarile multithreaded queue thing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.Runnable; | |
import java.net.Socket; | |
import java.util.concurrent.atomic.AtomicBoolean; | |
public class InputListenerThread implements Runnable { | |
private Socket socket; | |
private AtomicBoolean closing; | |
public InputListenerThread(Socket s, AtomicBoolean cl) { | |
socket = s; | |
closing = cl; | |
} | |
@Override | |
public void run() { | |
BufferedReader readIn = new BufferedReader(new InputStreamReader(System.in)); | |
String input; | |
try { | |
while (!closing && (input = in.readLine()) != null) { | |
// got input! push it on the send queue | |
StringBuilder s = new StringBuilder(); | |
// stringbuilder fastfast, but you could do just + too I guess | |
s.append("PRIVMSG "); | |
s.append(channel); | |
s.append(" :"); | |
s.append(input); | |
s.append("\r\n"); | |
socket.write(s.toString()); // or something | |
if (/* User sends Ctrl+C or something */) { | |
closing = true; | |
} | |
} | |
} catch (IOException e) { | |
System.out.println("stdin threw, input listener exiting!"); | |
closing = true; | |
} | |
} | |
} | |
public class NetworkListenerThread implements Runnable { | |
private Socket socket; | |
public NetworkListenerThread(Socket s) { | |
socket = s; | |
} | |
@Override | |
public void run() { | |
while (!closing && /*socket read stuff*/) { | |
// Listen to network here | |
// something something socket.read() | |
// System.out.println(line) | |
// do any other kind of network read / handling activity here | |
// or pass it to an UI thread via similar queuing if you wanna manythread | |
if (/* socket reads NULL, user sends Ctrl+C, Esc, writes Exit, god knows */) { | |
closing = true; | |
} | |
} | |
} | |
} | |
public class Main { | |
Socket networkSocket; | |
InputListenerThread inl; | |
NetworkListenerThread netl; | |
/** | |
Is the program closing? | |
The program closes on two occasions: | |
1) if NetworkListener detects a disconnect, or DONE line | |
2) if InputListener receives a Ctrl+C, or input disconnects | |
*/ | |
AtomicBoolean closing; | |
public static void main(String[] args) { | |
// TODO: set up a socket here | |
// note, normally you'd synchronize socket access via a lock of some sort | |
// but in this case, Java sockets can just read/write in parallel just fine | |
inl = new Thread(new InputListenerThread(socket, closing)); | |
netl = new Thread(new NetworkListenerThread(socket, closing)); | |
inl.start(); | |
netl.start(); | |
// wait until both threads have exited to go onward | |
netl.join(); | |
inl.join(); | |
// code here will run when both netl and inl have stopped | |
System.out.println("lol we're done here"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment