Created
December 17, 2014 17:28
-
-
Save olegchir/280a16685eac44a13f5e to your computer and use it in GitHub Desktop.
This file contains 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 javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.util.concurrent.BlockingQueue; | |
import java.util.concurrent.LinkedBlockingQueue; | |
class ParallelPrinter { | |
final BlockingQueue<String> messageQueue; | |
public ParallelPrinter(BlockingQueue<String> messageQueue) { | |
this.messageQueue = messageQueue; | |
} | |
public void poll() { | |
Thread running = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
while (true) { | |
try { | |
String data = messageQueue.take(); | |
System.out.println(data); | |
} catch (InterruptedException e) { | |
System.err.println("Error occurred:" + e); | |
} | |
} | |
} | |
}); | |
running.start(); | |
} | |
} | |
class SubscriberGui extends JFrame | |
{ | |
final BlockingQueue<String> messageQueue; | |
private String subscribe = ""; | |
private String answer; | |
public SubscriberGui(BlockingQueue<String> messageQueue) | |
{ | |
super("Frame"); | |
this.messageQueue = messageQueue; | |
initGui(); | |
} | |
private void initGui() | |
{ | |
setSize(400, 400); | |
setLayout(new BorderLayout()); | |
final JButton sendSubcrButton = new JButton("Send"); | |
final JTextField subscrLine = new JTextField(50); | |
JLabel label = new JLabel("Enter your data here:"); | |
JPanel inputPanel = new JPanel(); | |
inputPanel.add(label); | |
inputPanel.add(subscrLine); | |
add(inputPanel, BorderLayout.PAGE_START); | |
add(sendSubcrButton, BorderLayout.PAGE_END); | |
pack(); | |
setResizable(false); | |
sendSubcrButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if (e.getSource() == sendSubcrButton) | |
{ | |
subscribe = subscrLine.getText(); | |
messageQueue.offer(subscribe); | |
} | |
} | |
}); | |
setLocationRelativeTo(null); | |
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
} | |
} | |
public class Main { | |
public static void main(String[] args) throws InterruptedException { | |
final BlockingQueue<String> queue = new LinkedBlockingQueue<String>(); | |
ParallelPrinter parallelPrinter = new ParallelPrinter(queue); | |
parallelPrinter.poll(); | |
/** Gui in edt **/ | |
EventQueue.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
SubscriberGui sgui = new SubscriberGui(queue); | |
sgui.setVisible(true); | |
} | |
}); | |
System.out.println("stop"); //тут печатать | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment