Article View: borland.public.jbuilder.java.api
Article #3327Will thread degrade performance ??? Please help
From: Nhu Le
Date: Sat, 07 Feb 1998 00:00
Date: Sat, 07 Feb 1998 00:00
164 lines
4332 bytes
4332 bytes
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);
}
}
Technical Details
Message-ID:
<01bd304d$324d8c80$94c9a4c6@NHULE.NBNET.NB.CA>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,9841e447c1f2ff4b,start X-Google-Attributes: gid1089d0,public From: "Nhu Le" <g1kd@unb.ca> Subject: Will thread degrade performance ??? Please help Date: 1998/02/07 Message-ID: <01bd304d$324d8c80$94c9a4c6@NHULE.NBNET.NB.CA> X-Deja-AN: 322918324 Organization: University of New Brunswick Newsgroups: borland.public.jbuilder.java.api