Thread Assignment Confusion
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Thread Assignment Confusion

  1. #1
    Join Date
    Sep 2008

    Question Thread Assignment Confusion

    My assignment is to add threading to a Java program that creates an "Orbital Fractal" through a GUI that allows the viewer to adjust colors, number of dots, etc. I've got the basic program all finished from a previous assignment, but I'm lost in regards to adding threading.

    My basic confusion comes from the fact that almost every threading tutorial I've come across so far has the main class of the program using the threading. In the program we're doing, the main class runs everything, but only the drawing section of the application is supposed to be run in a thread.

    The drawing right now is done in this function:
    private void drawFractal(Graphics g, Dimension d)
    	double x = 0.0;
    	double y = 0.0;
    	int dmin = (d.width < d.height) ? d.width : d.height;
    	double xmin = 0.0;
    	double xmax = 0.0;
    	double ymin = 0.0;
    	double ymax = 0.0;
    	for (int dot = 0; dot < nDots; ++dot) {
    	    for (int dot2 = 0; dot2 < 1000; ++dot2) {
    		double x2 = nextX(x, y);
    		double y2 = nextY(x, y);
    		xmin = (x2 < xmin) ? x2 : xmin;
    		xmax = (x2 > xmax) ? x2 : xmax;
    		ymin = (y2 < ymin) ? y2 : ymin;
    		ymax = (y2 > ymax) ? y2 : ymax;
    		x = x2;
    		y = y2;
    	double dx = ((double) (d.width)) / (xmax - xmin);
    	double dy = ((double) (d.height)) / (ymax - ymin);
    	x = 0.0;
    	y = 0.0;
    	for (int dot = 0; dot < nDots; ++dot) {
    	    g.setColor (interpolate(color1, color2, dot, nDots));
    	    for (int dot2 = 0; dot2 < 1000; ++dot2) {
    		double x2 = nextX(x, y);
    		double y2 = nextY(x, y);
    		int dotx = (int) (dx * (x2 - xmin));
    		int doty = (int) (dy * (y2 - ymin));
    		g.drawLine (dotx, doty, dotx, doty);
    		x = x2;
    		y = y2;
    In order to make this drawing be done in a thread should I be changing the main class of the application to be threaded and somehow include the above drawing code in a run function, or is there some way to convert the above function to be run as a thread?

  2. #2
    Join Date
    Apr 2007

    Re: Thread Assignment Confusion

    Create a class that implements Runnable, place the method in that, and call it from the run method. Runnable will exit, when drawing is done. When ever user initiates a change, that would provoke calling the drawFractal method, use SwingUtils.invokeLater(...) to launch a new Thread (one of the runnables you created). Give it the right Graphics as param and you are home free.

    So basically you are assuring that each custom paint call is done in AWT-EDT, and executed asynchronously, at the earliest inconvinience. Or if you want to accertain that only one drawing is done at a time (might make sense), use executors singleThreadPool, which will accept the runnable and launch when available. Place the SwingUtils check in the run method of the runnable.

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999

    Re: Thread Assignment Confusion

    As Londbrok implies, you should do GUI tasks (e.g. Swing painting) in the Swing 'Event Despatch Thread' (EDT). In Java 5, this means using SwingUtilities.invokeLater(Runnable), which will wait until all Swing events have finished before running the Runnable in the EDT.

    In Java 6, you can use SwingWorker, which does the same kind of thing, but is easier and more flexible to use. With SwingWorker, you can either do a one-off background calculation and update the GUI when it finishes, or you can do continuous calculations in the background, and periodically update the GUI with the latest results.

    See 'How To Use Threads' for full details.

    Vague and nebulous is the beginning of all things, but not their end...
    K. Gibran
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Azure Activities Information Page

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center