Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
57
Task/Odd-word-problem/Java/odd-word-problem.java
Normal file
57
Task/Odd-word-problem/Java/odd-word-problem.java
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
public class OddWord {
|
||||
interface CharHandler {
|
||||
CharHandler handle(char c) throws Exception;
|
||||
}
|
||||
final CharHandler fwd = new CharHandler() {
|
||||
public CharHandler handle(char c) {
|
||||
System.out.print(c);
|
||||
return (Character.isLetter(c) ? fwd : rev);
|
||||
}
|
||||
};
|
||||
class Reverser extends Thread implements CharHandler {
|
||||
Reverser() {
|
||||
setDaemon(true);
|
||||
start();
|
||||
}
|
||||
private Character ch; // For inter-thread comms
|
||||
private char recur() throws Exception {
|
||||
notify();
|
||||
while (ch == null) wait();
|
||||
char c = ch, ret = c;
|
||||
ch = null;
|
||||
if (Character.isLetter(c)) {
|
||||
ret = recur();
|
||||
System.out.print(c);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
public synchronized void run() {
|
||||
try {
|
||||
while (true) {
|
||||
System.out.print(recur());
|
||||
notify();
|
||||
}
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
public synchronized CharHandler handle(char c) throws Exception {
|
||||
while (ch != null) wait();
|
||||
ch = c;
|
||||
notify();
|
||||
while (ch != null) wait();
|
||||
return (Character.isLetter(c) ? rev : fwd);
|
||||
}
|
||||
}
|
||||
final CharHandler rev = new Reverser();
|
||||
|
||||
public void loop() throws Exception {
|
||||
CharHandler handler = fwd;
|
||||
int c;
|
||||
while ((c = System.in.read()) >= 0) {
|
||||
handler = handler.handle((char) c);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new OddWord().loop();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue