Hey guys

I'm about to launch this server on my VPS and want to make sure it won't do any damage or hit my CPU cap.

I've written this code for my online game which visits a page every 888ms by starting a new thread which opens a connection with that page, then closes it.

It's really short and simple.

A few times when running this server on my laptop it has made it go INCREDIBLY HOT in under 20 seconds, i'm thinking maybe a memory leak or threads weren't closing?

How can i make this code use less CPU? Does this code have any flaws such as not terminating the thread? Thanks for your advice!

main.java

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class main {
static Boolean loop = true;




    
  public static void main(String[] args) {
      // Make the frame so that we can close the server easily
      JFrame frame = new JFrame("InvasionPoint Server");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      JLabel emptyLabel = new JLabel("Server running. Close this window to terminate server.");
      frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
      frame.setSize(350, 100);

    // Declare and start the timer which will poll the page every 888ms
    Timer timer = new Timer(888, new MyTimerActionListener());
    timer.start();
    }
  }



class MyTimerActionListener implements ActionListener {
    
    // Timer ticks, start the thread
  public void actionPerformed(ActionEvent e) {
      Thread thread = new timer1(); 
      thread.start(); 

  }
}

timer1.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;




public class timer1 extends Thread{
    
    // the thread for hitting the page
    public void run() {
        try {
        URL yahoo = new URL("http://localhost/invasionpoint/client/action/check.php?serverbypass=truetruetrue");
        
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
            System.out.println("true");
        in.close();
        }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Thannnks :)

Three questions:
How many lines are typically read from yahoo each time?
Why do you close the in stream inside the loop that's trying to read it?
Have you used task manager to see what CPU utilisation you are actually getting - what is it in %?

The page it polls prints nothing back to the Java server.
At the moment the server is sitting at 0.08% CPU.... ill fire some timers and see how much it takes up...

Ok wow it's still 0% rounded up.

It was around 50% before i removed that while loop in main.java and some other bits and bobs

This article has been dead for over six months. Start a new discussion instead.