Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wait for code block to finish / Synchronous code block 1

Status
Not open for further replies.

TheInsider

Programmer
Jul 17, 2000
796
CA
Hi,

I have a very simple application that consists of a JFrame with a menu bar. When the user performs certain actions, I enable or disable menu items accordingly, using the setEnable() Method within a function I created. i.e.
Code:
public class MyApp extends JFrame implements ActionListener{
{

...
//call to toggleMenuItem() from some function within MyApp class
toggleMenuItems();
...

//method defined WITHIN the same MyApp class
private void toggleMenuItems(){
... setEnable(true);
... setEnable(false);
... setEnable(true);
}

}
The problem is that Java is fairly slow and, to make matters worse, it's asynchronous. So, what happens is the user can click on the menu bar (say the "File" menu) before the toggleMenuItems code block has even finished executing. The result is that the items don't become enabled/disabled until several seconds after the menu drops down!

I tried playing with the synchronous/wait()/notifyAll() keywords and methods, but nothing seems to work properly.

I'm new to threading... and here Java is implicitly handling the threading... so basically how do I get the program to wait for a specific code block to finish executing before allowing the rest of the program to carry on?

Thanks
 
Thank you for replying. You're correct, it is "setEnabled" with a "d". That was simply a typo I made when I was writing the example above.

There are most likely two reasons why my code is slow: 1) I'm writing it on an Athlon 1.0Ghz and 2) I'm storing a reference to the individual menuItems withing a B-Tree/TreeMap so that I can look them up by Key value, rather than by their indexed position within the menu. The latter makes the code more of a pain to read and write.

I'm new to Java, so perhaps there is a better way of going about this... However, this is an image processing application using the JAI and that seems to bog it down some, so I am getting delays when I enable my menu items.

If I had my choice, I would write the entire application synchonously and/or in another language such as C++ or Visual Basic. However, that's not what my client wants :(

Any ideas on how to force a code block {} or function to finish synchronously before allowing Java to carry on?

Thanks
 
a) Regarding only the menu-quesstion, a Pentium with 100 Mhz should be sufficient.
b) A tree is an appropriate datatstructure to map a menu - this should improve performance, not reduce it.
Of course you woulndn't notice a difference, handling less than 10 000 MenuItems (which means 10 menus with 10 submenus each, 4 layers deep).

Working with big images on low memory might slow things down. Is your machine swapping?

I guess synchronizing your code will be a way to the wrong direction, but try it:

Code:
synchronized (WHAT)
{
    /* your block here*/
}

Here is an example which handles 10 000 MenuItems:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class ToggleMenu extends JFrame implements ActionListener
{
	private JTextField jtfa;
	private JTextArea jta;
	private JButton jb;
	private JMenu jm;
	private Map <String, JMenuItem> mim = new HashMap <String, JMenuItem> ();

	private void create10SubItems (int maxlayer, int layer, String name, JMenuItem parent)
	{
		for (int i = 0; i < 10; ++i)
		{
			String nname = name + i  + ".";
			JMenuItem jmi;
			if (layer < maxlayer)
			{
				jmi = new JMenu (nname);
				create10SubItems (maxlayer, layer+1, nname, jmi);
			}
			else
			{
				jmi = new JMenuItem (nname);
			}
			mim.put (nname, jmi);
			parent.add (jmi);
		}
	}

	public ToggleMenu ()
	{
		super ("ToggleMenu");
		JPanel mainpanel = new JPanel ();
		mainpanel.setLayout (new BorderLayout ());
		this.getContentPane ().add (mainpanel);

		jtfa = new JTextField ("3.4");
		jb = new JButton ("toggle");
		jb.addActionListener (this);
		jta = new JTextArea ();

		JMenuBar jmb = new JMenuBar ();
		jm = new JMenu ("FooMenu");
		create10SubItems (3, 0, "", jm);

		jmb.add (jm);
		setJMenuBar (jmb);

		mainpanel.add (jtfa, BorderLayout.NORTH);
		mainpanel.add (jta, BorderLayout.CENTER);
		mainpanel.add (jb, BorderLayout.SOUTH);

		setSize (400, 200);
		setLocation (100, 100);
		setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		setVisible (true);
	}

	private void toggleMenuItems (String muster)
	{
		for (Map.Entry <String, JMenuItem> me : mim.entrySet ())
		{
			String itemname = me.getKey ();
			if (itemname.indexOf (muster) >= 0)
			{
				JMenuItem jmi = me.getValue ();
				jmi.setEnabled (! jmi.isEnabled ());
			}
		}
	}

	public void actionPerformed (ActionEvent e)
	{
		String cmd = e.getActionCommand ();
		if (cmd.equals ("toggle"))
		{
			String muster = jtfa.getText ();
			jta.setText ("muster: " + muster);
			toggleMenuItems (muster);
		}
	}

	public static void main (String args[])
	{
		new ToggleMenu ();
	}
}

seeking a job as java-programmer in Berlin:
 
Thank you for your help. I think I'll skip the synchronization . Now that the application is 99.9% complete, the problem seems less noticable -- because there's more for the user to focus on than just staring at an empty form with a menu, as I had been doing before. This isn't a mission-critical application, so I don't think it's worth spending a lot of time tampering with the threading. And it's true, this machine isn't flush in RAM... XP Pro with 256MB.

Thanks again
 
Have you got a virus-checker running? We have Sophos running at work and it kills Java apps. I presume it's checking each time a class is loaded from a jar. I have to explicitly stop it monitoring any folders containting jars to get any kind of joy. This might explain your performance problems.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top