Skip to content

Instantly share code, notes, and snippets.

@azyobuzin
Created June 12, 2018 02:44
Show Gist options
  • Save azyobuzin/04e4f41bb02006c10efc1ad2ed45cd15 to your computer and use it in GitHub Desktop.
Save azyobuzin/04e4f41bb02006c10efc1ad2ed45cd15 to your computer and use it in GitHub Desktop.
PipedReader/Writer が遅いのか notify が遅いのか調べたけど PipedReader/Writer が遅かった
package dentaku.gui;
import javax.swing.*;
import java.awt.*;
import java.io.IOError;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
public class PipeTestApp {
public static void main(String[] args) throws IOException {
notifyTest();
}
private static void PipeTest() throws IOException {
JFrame frame = new JFrame();
frame.setSize(200, 200);
Container pane = frame.getContentPane();
PipedWriter writer = new PipedWriter();
PipedReader reader = new PipedReader(writer);
JButton btn = new JButton();
btn.setText("ボタン");
btn.addActionListener(e -> {
try {
writer.write('a');
} catch (IOException ex) {
throw new IOError(ex);
}
});
pane.add(btn, BorderLayout.NORTH);
JLabel label = new JLabel();
label.setText("a");
pane.add(label, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
new Thread(() -> {
try {
while(true) {
int c = reader.read();
if (c < 0) return;
label.setText(label.getText() + (char)c);
}
} catch (IOException e) {
throw new IOError(e);
}
}).start();
}
private static void notifyTest() {
JFrame frame = new JFrame();
frame.setSize(200, 200);
Container pane = frame.getContentPane();
Object lock = new Object();
JButton btn = new JButton();
btn.setText("ボタン");
btn.addActionListener(e -> {
synchronized (lock){
lock.notifyAll();
}
});
pane.add(btn, BorderLayout.NORTH);
JLabel label = new JLabel();
label.setText("a");
pane.add(label, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
new Thread(() -> {
int count = 0;
while(true) {
synchronized (lock) {
try {
lock.wait();
}catch(Throwable e){return;}
count++;
label.setText(Integer.toString(count));
}
}
}).start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment