Frage

(Working in NetBeans/Java) I use a button to retrieve values from an external source, and everything worked all well and fine with minor issues, then I started encountering a java.lang.OutOfMemoryError: unable to create new native thread exception

This application writes the external values to a JTable. This external values are public variables in other locations

I know it's bad practice to work with multiple JFrames, but I don't see the point in it to recreate a whole new Java Card or Option Pane/Dialog. I'm also determined to create a work-around to get the thing working.

What could be causing it and how do I fix it?

Here's the code. The exception points to Timer timer = new Timer();.

String username;
String password;

//...

final Apps.UserManager.NewUser newUser = new Apps.UserManager.NewUser();
newUser.setVisible(true);
newUser.requestFocus();

newUser.suVftrun = 0;

int delay = 10000; // delay for 10 sec.
int period = 1000; // repeat every sec.

do{
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
            int bcol1 = model.getColumnCount();
            int bcol2 = model.getRowCount();

            username = newUser.u2ftrun;
            password = newUser.p2ftrun;

            System.out.println(newUser.u2ftrun);
            System.out.println(newUser.p2ftrun);

            try {
                String tRecord1 = (String) jTable1.getValueAt(bcol1, bcol2);
                if (newUser.suVftrun == 1) {
                    if (!username.equals(null)) {
                        if (!tRecord1.equals(username)) {
                            model.addRow(new Object[]{username, password});
                            this.cancel();
                        } else {
                            System.out.println("Same Data");
                        }
                    } else {
                        System.out.println("No Data Received!");
                    }
                } else {
                    System.out.println("User hasn't been created yet");
                }
            } catch (Exception ex) {
                System.out.println("No values in table. Trying alternative");
                try {
                    if (newUser.suVftrun == 1) {
                        if (!username.equals(null)) {
                            try {
                                model.addRow(new Object[]{username, password});
                                this.cancel();
                            } catch (Exception e) {
                                System.out.println("Repeating...");
                            }
                        } else {
                            System.out.println("No Data Received!");
                        }
                    } else {
                        System.out.println("User hasn't been created yet");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }, delay, period);
} while (newUser.isVisible());
War es hilfreich?

Lösung

You have a massive issue. Wait, that's not big enough:

A Massive Issue

Better.

You are busy waiting in a do loop and spinning up a new Timer each time:

do {
    Timer timer = new Timer();
    //some other stuff
} while (newUser.isVisible());

This is causing thousands (millions?) of Timer instances to be created, each with its own Thread.

  1. never busy wait
  2. don't create thousands of Timer instances

Further, you are using the java.util.Timer not the javax.swing.Timer which means that the eventual action is not executed on the EDT.

This is a violation if the Swing threading policy. Swing objects, except in rare case, are not thread safe and should only be accessed from the EDT.

In short; there is very little that isn't wrong with this code.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top