Solved

BlockingQueue thread-safe?

Posted on 2013-11-06
2
487 Views
Last Modified: 2013-11-21
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

0
Comment
Question by:pdering
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 39629033
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.
0
 
LVL 27

Accepted Solution

by:
dpearson earned 500 total points
ID: 39629085
I agree this code looks basically fine.

One small suggestion is to consider using:

poll()

instead of:

take()

You may find it makes shutdown easier to handle.

E.g. You could have poll() with a timeout of 1 second and then check to see if a flag is set to indicate "shutdown please".  If not, then loop around and wait for the next object.

take() blocks forever, so the only way to handle shutdown there is either to completely kill the thread (not always what you want) or use a custom 'shutdown' task that you add to the queue - although that won't be seen until all other tasks ahead of it are completed.

As I say, just something to consider.

Doug
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 24
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO) 1 59
DTD and JAVA versions 1 30
JDBC performance 8 29
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question