>Delivered-To: [EMAIL PROTECTED]
>X-EM-Version: 5, 0, 0, 4
>X-EM-Registration: #01E0520310450300B900
>X-Priority: 3
>MIME-Version: 1.0
>Date: Thu, 11 Oct 2001 09:21:50 -0500
>From: Sam <[EMAIL PROTECTED]>
>Subject: [JAVA3D] a CPU utilization question
>To: [EMAIL PROTECTED]
>
>Hi,
>
>I want to decrease the CPU utilization of my Java 3D application. I came
>across an answer to by problem  when I was going through the Java 3D FAQ
>page and I have pasted it below:
>
>
>"How can I stop 100% CPU utilization?
>
>Java3D uses 100% CPU to gain maximum performance and number of frames per
>second rendered. On games and many highly complex applications, that leaves
>no time for user code to do stuff.
>To modify the priority of all the Java 3D threads running you can use the
>static setJ3DThreadPriority() method. The values range from
>Thread.MIN_PRIORITY to Thread.MAX_PRIORITY .   "
>
>
>But, I am still not sure which Java 3D  threads are refered to in the above
>statement.  Could someone explain what this process involved in a bit more
>detail please?
>
This  setJ3DThreadPriority() apply to ALL Java3D internal threads.
Here is an example to show Threads running under Java3D and
setting the priority.

- Kelvin

/*
 *    "@(#)ThreadPriority.java 1.1 99/10/27"
 *
 * Copyright (c) 1996-1999 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class ThreadPriority extends JApplet {

    JTextArea textarea = new JTextArea(20, 15);

    public BranchGroup createSceneGraph() {
        // Create the root of the branch graph
        BranchGroup objRoot = new BranchGroup();

        // Create the transform group node and initialize it to the
        // identity.  Enable the TRANSFORM_WRITE capability so that
        // our behavior code can modify it at runtime.  Add it to the
        // root of the subgraph.
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);

        // Create a simple shape leaf node, add it to the scene graph.
        objTrans.addChild(new ColorCube(0.4));

        // Create a new Behavior object that will perform the desired
        // operation on the specified transform object and add it into
        // the scene graph.
        Transform3D yAxis = new Transform3D();
        Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
                                        0, 0,
                                        4000, 0, 0,
                                        0, 0, 0);

        RotationInterpolator rotator =
            new RotationInterpolator(rotationAlpha, objTrans, yAxis,
                                     0.0f, (float) Math.PI*2.0f);
        BoundingSphere bounds =
            new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
        rotator.setSchedulingBounds(bounds);
        objTrans.addChild(rotator);

        // Have Java 3D perform optimizations on this scene graph.
        objRoot.compile();

        return objRoot;
    }

    public ThreadPriority() {

        Container contentPane = getContentPane();
        GraphicsConfiguration config =
           SimpleUniverse.getPreferredConfiguration();

        Canvas3D c = new Canvas3D(config);
        contentPane.add("Center", c);

        // Create a simple scene and attach it to the virtual universe
        BranchGroup scene = createSceneGraph();
        SimpleUniverse u = new SimpleUniverse(c);
        View view = u.getViewer().getView();
        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        u.getViewingPlatform().setNominalViewingTransform();

        u.addBranchGraph(scene);

        textarea = new JTextArea(20, 15);
        textarea.setEditable(false);

        String priorities[] = new String[Thread.MAX_PRIORITY -
                                      Thread.MIN_PRIORITY +1];
        for (int i=0; i < priorities.length; i++) {
            priorities[i] = i + Thread.MIN_PRIORITY + "";
        }
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(new JLabel("Set Java3D Priority "));
        final JComboBox j3dPri = new JComboBox(priorities);

        j3dPri.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                VirtualUniverse.setJ3DThreadPriority(j3dPri.getSelectedIndex()+
                                       Thread.MIN_PRIORITY);
                printThreadsInfo();
            }
        });

        panel.add(j3dPri);

        JPanel ppanel = new JPanel();
        ppanel.setLayout(new BorderLayout());
        ppanel.setBorder(new TitledBorder("Thread Priority"));
        ppanel.add("North", panel);
        ppanel.add("Center", new JScrollPane(textarea));
        contentPane.add("South", ppanel);
        j3dPri.setSelectedIndex(VirtualUniverse.getJ3DThreadPriority() - 
Thread.MIN_PRIORITY);


    }


    void printThreadsInfo() {

        ThreadGroup rootThreadGroup = Thread.currentThread().getThreadGroup();
        ThreadGroup parent;

        while ((parent = rootThreadGroup.getParent()) != null) {
            rootThreadGroup = parent;
        }

        ThreadGroup j3dThreadGroup = findThreadGroup(rootThreadGroup);

        if (j3dThreadGroup != null) {
            textarea.setText("");
            Thread threads[] = new Thread[j3dThreadGroup.activeCount()];
            int count = j3dThreadGroup.enumerate(threads);
            for (int i=0; i < count; i++) {
                textarea.append(threads[i].getName() + ": " +
                                threads[i].getPriority() + "\n");
            }
        }
    }


    ThreadGroup findThreadGroup(ThreadGroup group) {
        if (group.getName().equals("Java3D")) {
            return group;
        }

        ThreadGroup threadGroups[] =
                  new ThreadGroup[group.activeGroupCount()];
        int count = group.enumerate(threadGroups);
        ThreadGroup j3dGroup;
        for (int i=0; i < count; i++) {
            j3dGroup = findThreadGroup(threadGroups[i]);
            if (j3dGroup != null) {
                return j3dGroup;
            }
        }
        return null;
    }

    //
    // The following allows ThreadPriority to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
        ThreadPriority prog = new ThreadPriority();

        new MainFrame(prog, 350, 600);

        // wait until Java3D Thread setup complete
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {};

        prog.printThreadsInfo();
    }
}

Reply via email to