Link to home
Start Free TrialLog in
Avatar of pdering
pdering

asked on

BlockingQueue thread-safe?

Am I passing the BlockingQueue to frame2 properly?
Am I utilizing BlockingQueue in a thread-safe way?

frame1.java....

package package1;

import java.awt.BorderLayout;

public class frame1 extends JFrame {

	private JPanel contentPane;
	private BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame1 frame = new frame1();
					frame.setVisible(true);
					frame.Go();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		
		});
	}

	private void Go() {
		// TODO Auto-generated method stub
		SwingWorker<String,String> sw = new SwingWorker<String,String>() {

			@Override
			protected String doInBackground() throws Exception {
				// TODO Auto-generated method stub
				while(true)
					{
					publish(queue.take());
					}

			}

			@Override
			protected void done() {
				// TODO Auto-generated method stub
				super.done();
			}

			@Override
			protected void process(List<String> arg0) {
				// TODO Auto-generated method stub
				textField.setText(arg0.get(0));
				super.process(arg0);
			}
		};
		sw.execute();
		
	}

	/**
	 * Create the frame.
	 */
	public frame1() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		
		textField = new JTextField();
		panel.add(textField);
		textField.setColumns(10);
		
		JButton btnLaunchFrame = new JButton("Launch frame2");
		btnLaunchFrame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame2 f = new frame2(queue);
				f.setVisible(true);
			}
		});
		panel.add(btnLaunchFrame);
	}

}

Open in new window


frame2.java...

package package1;

import java.awt.BorderLayout;

public class frame2 extends JFrame {

	private JPanel contentPane;


	/**
	 * Create the frame.
	 */
	public frame2(final BlockingQueue<String> queue) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 244, 146);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		
		JButton btnClose = new JButton("Close and update queue");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					queue.put("hello");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				setVisible(false);
			}
		});
		panel.add(btnClose);
	}

}

Open in new window

Avatar of mccarl
mccarl
Flag of Australia image

Am I passing the BlockingQueue to frame2 properly?
Yes, that appears fine to me.

Am I utilizing BlockingQueue in a thread-safe way?
Yes, that also is fine. The idea of a BlockingQueue is to be able to use it across multiple threads in the way that you have done.
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial