🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #3332

Re: Will thread degrade performance ??? Please help

#3332
From: Rich Wilkman
Date: Mon, 09 Feb 1998 00:00
194 lines
5754 bytes
Nhu:

Threads won't make the individual task on the thread any faster but it
will allow the sum total of tasks to possibly complete faster.

Since you are doing an Applet, you should also be aware that the level
of Thread support in the browsers varies.  Older browsers (esp. on the
*nix platforms) would simply ignore the threads and chain all the
processes in a linear fashion (and still run 4 times slower then the
same code without threads).

-Rich

Nhu Le wrote:
> 
> I am trying to play with thread in java now.  I create 5 seperate threads
> and then let them chase after my mouse clicks.  However, the performance is
> unacceptable.  I am running on P150 - 64M - WindowsNT(SP1).  Below is the
> code, please tell me all the bad things I did that killed the performance.
> 
> Thanks
> 
> package untitled3;
> 
> import java.awt.*;
> import java.awt.event.*;
> import java.applet.*;
> import borland.jbcl.layout.*;
> import borland.jbcl.control.*;
> 
> class Element extends Label implements Runnable
>   {
>   int itsX, itsY;
>   int tarX, tarY;
>   Thread itsThread;
> 
>   public void start()
>     {
>     itsThread = new Thread(this);
>     itsThread.start();
>     }
>   public void setTarget(int x, int y)
>     {
>     tarX = x;
>     tarY = y;
>     }
>   public void setText(char c)
>     {
>     super.setText(new String(new Character(c).toString()));
>     }
> 
>   public void run()
>     {
>     itsX = getLocation().x;
>     itsY = getLocation().y;
>     while (true)
>       {
>       if (itsX < tarX) itsX++;
>       if (itsX > tarX) itsX--;
>       if (itsY < tarY) itsY++;
>       if (itsY > tarY) itsY--;
>       try{Thread.sleep(30);}catch(InterruptedException Ie){};
>       setLocation(new Point(itsX, itsY));
>       }
>     }
>   }//class Element
> 
> public class Applet1 extends Applet {
>   XYLayout xYLayout1 = new XYLayout();
>   boolean isStandalone = false;
>   TextField Input = new TextField();
>   Button GetButton = new Button();
>   Element a[] = new Element[5];
>   boolean isStarted = false;
> 
>   //Get a parameter value
>   public String getParameter(String key, String def) {
>     return isStandalone ? System.getProperty(key, def) :
>       (getParameter(key) != null ? getParameter(key) : def);
>   }
> 
>   //Construct the applet
>   public Applet1() {
>   }
> 
>   //Initialize the applet
>   public void init() {
>     try { jbInit(); } catch (Exception e) { e.printStackTrace(); }
>   }
> 
>   //Component initialization
>   public void jbInit() throws Exception{
>     this.addMouseListener(new Applet1_this_mouseAdapter(this));
>     xYLayout1.setWidth(500);
>     xYLayout1.setHeight(500);
>     GetButton.setLabel("Get");
>     GetButton.addMouseListener(new Applet1_GetButton_mouseAdapter(this));
>     this.setLayout(xYLayout1);
>     this.add(Input, new XYConstraints(32, 420, 237, 24));
>     this.add(GetButton, new XYConstraints(276, 421, 52, -1));
>     int i;
>     for (i = 0; i < 5; i++)
>       {
>       a[i] = new Element();
>       this.add(a[i], new XYConstraints(i*23, 42, 23, 23));
>       }
>   }
> 
>   //Start the applet
>   public void start() {
>   }
> 
>   //Stop the applet
>   public void stop() {
>   }
> 
>   //Destroy the applet
>   public void destroy() {
>   }
> 
>   //Get Applet information
>   public String getAppletInfo() {
>     return "Applet Information";
>   }
> 
>   //Get parameter info
>   public String[][] getParameterInfo() {
>     return null;
>   }
> 
>   void GetButton_mouseClicked(MouseEvent e) {
>     int i;
>     String aTemp = Input.getText();
>     for (i = 0; i < aTemp.length(); i++)
>       {
>       a[i].setText(Input.getText().charAt(i));
>       }
>   }
> 
>   void this_mouseClicked(MouseEvent e) {
>     int i;
>     if (!isStarted){
>       for (i = 0; i < 5; i++)
>         {
>         a[i].start();
>         }
>       isStarted = true;
>       }
>     for (i = 0; i < 5; ++i)
>       {
>       a[i].setTarget(e.getX() + i * 23, e.getY());
>       }
>   }
> }
> 
> class Applet1_GetButton_mouseAdapter extends java.awt.event.MouseAdapter {
>   Applet1 adaptee;
> 
>   Applet1_GetButton_mouseAdapter(Applet1 adaptee) {
>     this.adaptee = adaptee;
>   }
> 
>   public void mouseClicked(MouseEvent e) {
>     adaptee.GetButton_mouseClicked(e);
>   }
> }
> 
> class Applet1_this_mouseAdapter extends java.awt.event.MouseAdapter {
>   Applet1 adaptee;
> 
>   Applet1_this_mouseAdapter(Applet1 adaptee) {
>     this.adaptee = adaptee;
>   }
> 
>   public void mouseClicked(MouseEvent e) {
>     adaptee.this_mouseClicked(e);
>   }
> }

-- 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Rich Wilkman
Senior Quality Assurance Engineer
Borland JBuilder Team

If you see something that looks like an opinion up there,
it's mine and mine alone.

Mail sent to me from the newsgroups won't bounce but will
be ignored.  If I have time, I'll respond, but you  have
a better chance if you confine your posts to the newsgroups
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Technical Details
Message-ID: <34DF408A.E4B9216C@corp.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <01bd304d$324d8c80$94c9a4c6@NHULE.NBNET.NB.CA>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,9841e447c1f2ff4b
X-Google-Attributes: gid1089d0,public
From: Rich Wilkman <rwilkman@corp.borland.com>
Subject: Re: Will thread degrade performance ??? Please help
Date: 1998/02/09
Message-ID: <34DF408A.E4B9216C@corp.borland.com>
X-Deja-AN: 323485909
Content-Transfer-Encoding: 7bit
References: <01bd304d$324d8c80$94c9a4c6@NHULE.NBNET.NB.CA>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Organization: None
Newsgroups: borland.public.jbuilder.java.api