• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java thread runs slow at random runs!

Morfik

Member
I am a beginner java programmer.

I have recently written an Arkanoid game using JFrame and Swing etc. I have a large Thread (the type with Runnable) that hadnles a lot of things in its "run" method.

Now the same file sometimes when I compile the Thread runs at the speed it is supposed to. Now if I compile the same file again, the Thread is sometimes slow, but everything does function properly (ie: the ball is very slow).

What could be causing this?

I am using a Sun SDK and XP pro command prompt to compile and run....


I will post the code if necessary...

thanx.
 
Watch the CPU utilization and the processes that are running. I'd bet that it isn't so much the compile that is causing the problem but that you have a program stuck in a loop in the background that you didn't quit completely.
 
Yes, that sounds like the only reasonable cause...

BUT, once I get the "bad" compile. I can log off of my current computer (@ school) and then log back on another day and it would still be slow. Once it does that, it doesn't really matter how long I set the "Sleep Time" of my thread. At that point the only fix is "lucky" compile without changing anything.

Also it sometimes works on my computer, but when tranferred to my teacher's it has never worked. I even tried to transfer a compiled class file that works fine on mine and then once it is on my teacher's pc the ball will be the same slow speed no matter howmany times he tries to recompile etc...

Very weired problem, i have never seen anything of this sort this year (my 1st java year).

PPPLLLEEEEEZZZZ HHHHHEEEEELLLLLLPPPPPP.

thanx
 
Are you using just this single thread or are there mutliple threads involved? It may be race conditions or somehow you are depending on time somewhere? I would place a bet that it's something in your code and not your compiler. If it is isn't huge, I would post the code
 
Here is the code, I know it is long... but it would mean a lot to me if someone tried to help me out here... It is for an independent study project at my high school and I need to present it in a few days. Can't have it run superslow in the presentation. The code is not properly explained, but I would be glad to answer any questions. Thanx.

update: look at singh's post for the code (thanx to him for formatting it)...
 
So just post links to the source code, hosted somewhere else for download.

I'm sad to report that I'm a bit too lazy to analyze your program this evening. :frown:
 
Just helping out by fixing the formatting a bit...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Arkanoid extends JFrame
implements MouseListener,
WindowListener,
MouseMotionListener,
Runnable
{
private final int GAME_STATE_TITLE_SCREEN = 1;
private final int GAME_STATE_GAME = 2;
private final int GAME_STATE_GAME_OVER = 3;

private int gameState;

private int racketXPos;
private int racketYPos;

private int ballXPos;
private int ballYPos;

private int remainingBalls;

private int currentXPos;
private int currentYPos;

private int oldX;
private int oldY;

private int ballXIncrement;
private int ballYIncrement;

private int[][] xBlockArray = new int[6][16];
private int[][] yBlockArray = new int[6][16];

private Thread ballThread = new Thread(this);
private int sleepTime = 4;

private boolean drawWithBall;
private boolean firstRun; //no start command sent during later runs

private String currentLevel = "Level One";

Color[][] colorBlockArray = new Color[6][16];

public Arkanoid()
{
this.addMouseListener(this);
this.addWindowListener(this);
this.addMouseMotionListener(this);
gameState = GAME_STATE_TITLE_SCREEN;
drawWithBall = true;
firstRun = true;
ballXPos = 310;
ballYPos = 505;
remainingBalls = 4;
ballXIncrement = 1;
ballYIncrement = -1;
racketXPos = 280;
racketYPos = 540;
this.setSize(600, 600);
this.setBackground(Color.white);
this.show();
}

public void fillColorArray()
{
for (int x = 1; x <= 5; x++)
{
for (int y = 1; y <= 15; y++)
{
int red = (int)(Math.random() * 256);
int green = (int)(Math.random() * 256);
int blue = (int)(Math.random() * 256);
Color randomColor = new Color (red, green, blue);
colorBlockArray[x][y] = randomColor;
}
}
}

public void fillBlockArrays()
{
currentXPos = 30;
currentYPos = 150;
for (int x = 1; x <= 5; x++)
{
for (int y = 1; y <= 15; y++)
{
xBlockArray[x][y] = currentXPos;
yBlockArray[x][y] = currentYPos;
currentXPos += 36;
}
currentXPos = 30;
currentYPos += 20;
}
}

public void drawBlocks()
{
Graphics gfx = this.getGraphics();
int sumOfEmptyBlocks = 0;
for (int x = 1; x <= 5; x++)
{
for (int y = 1; y <= 15; y++)
{
if (xBlockArray[x][y] == 0 && yBlockArray[x][y] == 0)
{
gfx.setColor(Color.white);
gfx.fillRect(oldX, oldY, 36, 20);
sumOfEmptyBlocks++;
if (sumOfEmptyBlocks >= 75)
{
ballThread = null;
drawWithBall = true;
firstRun = true;
ballXIncrement *= -1;
ballYIncrement *= -1;
if (currentLevel.equals("Level One") == true)
{
currentLevel = "Level Two";
sleepTime--;
}
else if (currentLevel.equals("Level Two") == true)
{
currentLevel = "Level Three";
sleepTime--;
}
else if (currentLevel.equals("Level Three") == true)
{
currentLevel = "Level Four";
sleepTime--;
}
else
{
gameState = GAME_STATE_GAME_OVER;
}
this.repaint();
}
}
else
{
Color blockColor = colorBlockArray[x][y];
gfx.setColor(blockColor);
gfx.fill3DRect(xBlockArray[x][y], yBlockArray[x][y], 36, 20, true);
}
}
}
}

public void start()
{
ballThread.start();
}


public void stop()
{
ballThread = null;
}


public void destroy()
{

}

public void run()
{
Graphics gfx = this.getGraphics();
if (gameState == GAME_STATE_GAME)
{
while (ballThread != null)
{

//BOUNCE AND BREAK IN RESPONCE TO HITTING BLOCKS
if (ballYPos >= 130 && ballYPos <= 250)
{
for (int x = 1; x <= 5; x++)
{
for (int y = 1; y <= 15; y++)
{
if (xBlockArray[x][y] == 0 && yBlockArray[x][y] == 0)
;
else
{
///TTTOOOPPPPP
if (ballYPos + 12 == yBlockArray[x][y] &&
ballXPos + 12 >= xBlockArray[x][y] &&
ballXPos <= xBlockArray[x][y] + 36)
{
ballYIncrement *= -1;
oldX = xBlockArray[x][y];
xBlockArray[x][y] = 0;
oldY = yBlockArray[x][y];
yBlockArray[x][y] = 0;
this.drawBlocks();
break;
}

//bbbboooottttooommmm
else if

(ballYPos == yBlockArray[x][y] + 20 &&
ballXPos + 12 >= xBlockArray[x][y] &&
ballXPos <= xBlockArray[x][y] + 36)
{
ballYIncrement *= -1;
oldX = xBlockArray[x][y];
xBlockArray[x][y] = 0;
oldY = yBlockArray[x][y];
yBlockArray[x][y] = 0;
this.drawBlocks();
break;
}

///RRRIIGGGGHHHHTTTT
else if
(ballYPos + 12 >= yBlockArray[x][y] &&
ballYPos <= yBlockArray[x][y] + 20 &&
ballXPos + 12 == xBlockArray[x][y])
{
ballXIncrement *= -1;
oldX = xBlockArray[x][y];
xBlockArray[x][y] = 0;
oldY = yBlockArray[x][y];
yBlockArray[x][y] = 0;
this.drawBlocks();
break;
}

//lllleeeeffffttttt
else if

(ballYPos + 12 >= yBlockArray[x][y] &&
ballYPos <= yBlockArray[x][y] + 20 &&
ballXPos == xBlockArray[x][y] + 36)
{
ballXIncrement *= -1;
oldX = xBlockArray[x][y];
xBlockArray[x][y] = 0;
oldY = yBlockArray[x][y];
yBlockArray[x][y] = 0;
this.drawBlocks();
break;
}
}
}
}
}

this.drawBlocks();

//BOUNCE FROM WALLS AND RACKET
if (ballXPos >= 556)
{
ballXIncrement *= -1;
}

if (ballXPos <= 32)
{
ballXIncrement *= -1;
}

if (ballYPos <= 58)
{
ballYIncrement *= -1;
}

if (ballYPos >= 526)
{
if (ballXPos >= racketXPos + 5 && ballXPos <= racketXPos + 55)
{
ballYIncrement *= -1;
}
if (ballXPos >= racketXPos && ballXPos <= racketXPos + 5)
{
ballYIncrement *= -1;
ballXIncrement *= -1;
}
if (ballXPos >= racketXPos + 55 && ballXPos <= racketXPos + 60)
{
ballYIncrement *= -1;
ballXIncrement *= -1;
}

if (ballYPos >= racketYPos && ballYPos <= racketYPos + 10)
{
if (ballXPos == racketXPos)
ballXIncrement *= -1;
if (ballXPos == racketXPos + 60)
ballXIncrement *= -1;
}
}

//IF A BALL IS LOST
if (ballYPos >= 537)
{
stop();
remainingBalls--;

if (remainingBalls <= 0)
{
gameState = GAME_STATE_GAME_OVER;
this.repaint();
}

drawWithBall = true;
firstRun = true;
ballXIncrement *= -1;
ballYIncrement *= -1;
}

try
{
Thread.sleep(sleepTime);
}
catch(InterruptedException ex)
{

}

gfx.setColor(Color.white);
gfx.fillArc(ballXPos, ballYPos, 12, 12, 0, 360);
gfx.fillArc(ballXPos - ballXIncrement, ballYPos - ballYIncrement, 12, 12, 0, 360);
gfx.fillArc(ballXPos - 2 * ballXIncrement, ballYPos - 2 * ballYIncrement, 12, 12, 0, 360);

ballYPos += ballYIncrement;
ballXPos += ballXIncrement;

gfx.setColor(Color.black);
gfx.fillArc(ballXPos, ballYPos, 12, 12, 0, 360);

}
}
}

public static void main(String [] args)
{
Arkanoid gameWindow = new Arkanoid();
}

public void drawTitleScreen()
{
Container workArea = this.getContentPane();
int height = workArea.getHeight();
int width = workArea.getWidth();
Graphics gfx = workArea.getGraphics();
gfx.setColor(Color.black);
gfx.fillRect(0, 0, width, height);
gfx.setColor(Color.green);
gfx.drawString("ARKANOID", 265, 240);
gfx.drawString("By Saman Mirkazemi", 238, 455);
gfx.setColor(Color.gray);
gfx.drawString("click mouse to continue", 230, 480);
}

private void game()
{
if (firstRun == true)
{
this.fillColorArray();
this.fillBlockArrays();
this.drawBlocks();
}
Container workArea = this.getContentPane();
Graphics gfx = workArea.getGraphics();
int height = workArea.getHeight();
int width = workArea.getWidth();
gfx.setColor(Color.white);
gfx.fillRect(0, 0, width, height);
gfx.setColor(Color.black);
gfx.drawRect(25, 25, 541, 525);
}

public void gameOver()
{
Container workArea = this.getContentPane();
this.setBackground(Color.black);
Graphics gfx = workArea.getGraphics();
gfx.setColor(Color.black);
gfx.fillRect(0, 0, workArea.getWidth(), workArea.getHeight());
gfx.setColor(Color.green);
gfx.drawString("GAME OVER", 260, 240);
gfx.setColor(Color.gray);
gfx.drawString("REPLAY", 274, 465);
gfx.drawString("EXIT", 283, 480);
}

public void paint(Graphics gfx)
{
if (gameState == GAME_STATE_TITLE_SCREEN)
{
this.drawTitleScreen();
}
if (gameState == GAME_STATE_GAME)
{
this.game();
}
if (gameState == GAME_STATE_GAME_OVER)
{
this.gameOver();
}
}

public void update(Graphics gfx)
{
paint(gfx);
}

public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e)
{
gameState += 1; ////////////////////////////////////////////////
repaint(); ////////////////////////////////////////////////
}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e)
{
Graphics gfx = this.getGraphics();
if (e.getX() >= racketXPos && e.getX() <= racketXPos + 60
&& e.getY() >= racketYPos && e.getY() <= racketYPos + 30)
{
drawWithBall = false;
}
if (gameState == GAME_STATE_TITLE_SCREEN)
{
gameState = GAME_STATE_GAME;
this.repaint();
}
if (gameState == GAME_STATE_GAME_OVER)
{
this.dispose();
}
}

public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e)
{
Graphics gfx = this.getGraphics();
if (gameState == GAME_STATE_GAME)
{
if (e.getX() <= 59 || e.getX() >= 541)
;
else
{
gfx.clearRect(racketXPos, racketYPos, 60, 10);
racketXPos = e.getX() - 30;
if (drawWithBall == true)
{
gfx.setColor(Color.white);
gfx.fillArc(ballXPos, ballYPos, 12, 12, 0, 360);
ballXPos = racketXPos + 25;
ballYPos = racketYPos - 15;
gfx.setColor(Color.blue);
gfx.drawString("CLICK RACKET TO START", 230, 400);
gfx.drawString(currentLevel, 275, 415);

this.drawBlocks();
}
gfx.setColor(Color.black);
gfx.fillRect(racketXPos, racketYPos, 60, 10);
gfx.fillArc(ballXPos, ballYPos, 12, 12, 0, 360);

if (drawWithBall == false && firstRun == true)
{
gfx.setColor(Color.white);
gfx.drawString("CLICK RACKET TO START", 230, 400);
gfx.drawString(currentLevel, 275, 415);

firstRun = false;
if (ballThread == null)
ballThread = new Thread(this);
ballThread.start();

//DRAW REMAINING BALLS ON THE BOTTOM OF SCREEN

int x = 30;
int y = 555;
int tempX = 30;

for (int z = 1; z <= remainingBalls - 1; z++)
{
for (int h = 1; h <= 3; h++)
{
gfx.setColor(Color.white);
gfx.fillArc(tempX, y, 12, 12, 0, 360);
tempX += 15;
}
gfx.setColor(Color.red);
gfx.fillArc(x, y, 12, 12, 0, 360);
x += 15;
}
if (remainingBalls <= 1)
{
gfx.setColor(Color.white);
gfx.fillArc(30, 555, 12, 12, 0, 360);
}
}
}
}

if (gameState = GAME_STATE_GAMEOVER)
{
if (e.getX() <= 274 || e.getX() >= 541)
{
gfx.setColor(Color.yellow);
gfx.drawString("REPLAY", 274, 465);
}

if (e.getX() <= 59 || e.getX() >= 541)
{
gfx.setColor(Color.yellow);
gfx.drawString("EXIT", 283, 480);
}
}
}

public void windowActivated(WindowEvent e)
{

//DRAW REMAINING BALLS ON THE BOTTOM OF SCREEN

Graphics gfx = this.getGraphics();

int x = 30;
int y = 555;
int tempX = 30;

for (int z = 1; z <= remainingBalls - 1; z++)
{
for (int h = 1; h <= 3; h++)
{
gfx.setColor(Color.white);
gfx.fillArc(tempX, y, 12, 12, 0, 360);
tempX += 15;
}
gfx.setColor(Color.red);
gfx.fillArc(x, y, 12, 12, 0, 360);
x += 15;
}
if (remainingBalls <= 1)
{
gfx.setColor(Color.white);
gfx.fillArc(30, 555, 12, 12, 0, 360);
}

}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e)
{
ballThread = null;
this.dispose();
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}




 
wow, thank you so much for doing all that work. I was just trying to post a link to the file on my web site server. I tried to do an ftp link, but it didn't work...

I'll now delete my original, just to reduce the scrolling a bit.
 
I tried out the game.

I'm not noticing any slowdown. I'm using JRE version 1.4.2_03 to run the compiled app. I'm running it on a P4 1.4Ghz with 640MB of ram.

Which version of jdk are you using?


It's pretty cool by the way (code is very clean for a beginner), I wish I knew how to use a computer in high school. Good job.
 
Also, the posted code had 2 errors:

1) Do a find on Kind GAME_STATE_GAME_OVER, I think it should be GAME_STATE_GAME_OVER
2) On the same line the if statement has only 1 "=", which would give you compiler error, change it to "=="
 
Originally posted by: Punamo
I tried out the game.

I'm not noticing any slowdown. I'm using JRE version 1.4.2_03 to run the compiled app. I'm running it on a P4 1.4Ghz with 640MB of ram.

Which version of jdk are you using?


It's pretty cool by the way (code is very clean for a beginner), I wish I knew how to use a computer in high school. Good job.

for that I have to check back at school... in a few hours.
 
Originally posted by: Punamo
Also, the posted code had 2 errors:

1) Do a find on Kind GAME_STATE_GAME_OVER, I think it should be GAME_STATE_GAME_OVER
2) On the same line the if statement has only 1 "=", which would give you compiler error, change it to "=="

Yes, thanx.

This was actually the last edit I did before compiling it again... (menu options at the end: replay and exit)

also I forgot to mention if you want to run the code you have to take out a couple of lines that have "////////////////////////" in them. Those were just test code so I wouldn't have to play the whole game in order to get to the "game-over" screen.

 
Back
Top