Update: So you want to wait for the user to to input something from the GUI. This is possible but needs to be synchronized since the GUI runs in another thread.
So the steps are:
- Create an "holder" object that deligates the result from GUI to "logic" thread
- The "logic" thread waits for the input (using
holder.wait()
)
- When the user have entered text it synchronizes the "holder" object and gives the result + notifies the "logic" thread (with
holder.notify()
)
- The "logic" thread is released from its lock and continues.
Full example:
public static void main(String... args) throws Exception {
final List<Integer> holder = new LinkedList<Integer>();
final JFrame frame = new JFrame("Test");
final JTextField field = new JTextField("Enter some int + press enter");
frame.add(field);
field.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
synchronized (holder) {
holder.add(Integer.parseInt(field.getText()));
holder.notify();
}
frame.dispose();
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
// "logic" thread
synchronized (holder) {
// wait for input from field
while (holder.isEmpty())
holder.wait();
int nextInt = holder.remove(0);
System.out.println(nextInt);
//....
}
}