Jump to content

java new thread can't call function

technovore
Go to solution Solved by technovore,

magic happened, it works.
dbc is an instance of an object with a public boolean connectionWorks()

private void addDatabaseConnectionThread(){        t = new Thread() {            @[member=OverRide]            public void run() {                while (!interrupted() && threadRunning) {                    if (dbc.connectionWorks()){                        lblDBConnection.setText("");                    } else {                        lblDBConnection.setText("database connection failed");                    }                    try {                        Thread.sleep(3000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        };        t.start();    }

don't ask me why this works and the previeous one didn't.

I'm writing a program in java that uses a databaseconnection.

I wrote a new thread to constantly check wether I have a connection to the database.

my thread works, but when I call a function it doesn't run.

 

this works:

private void addDatabaseConnectionThread(){        (new Thread(){            @[member=OverRide]            public void run() {                while(!interrupted()){                    //if (dbc.connectionWorks()){                        System.out.println("true");                    //}                    try {                        Thread.sleep(200);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }).start();    }

but this doesn't work:

private void addDatabaseConnectionThread(){        (new Thread(){            @[member=OverRide]            public void run() {                while(!interrupted()){                    if (dbc.connectionWorks()){                        System.out.println("true");                    }                    try {                        Thread.sleep(200);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }).start();    }

and I have no idea why...

 

dbc.connectionWorks is a function in another class that works when called outside of the thread.

 

can anyone help?

Grammar nazis are people too!
Treat your local grammar nazi nicely and he might teach you a thing or two. (Note that I'm Belgian and not a native English speaker.)
Chivalry isn't dead!

Link to comment
Share on other sites

Link to post
Share on other sites

What error is it giving you? Is it throwing an error? Is the method you are calling static? Maybe make the thread static? We need to know more information about "not running"

Link to comment
Share on other sites

Link to post
Share on other sites

How do you know it doesn't work? Is it possible it's returning false?

What happens when you change your addDatabaseConnectionsThread method to the following?

private void addDatabaseConnectionThread(){     (new Thread(){         @[member=OverRide]         public void run() {             while(!interrupted()){                 System.out.println(dbc.connectionWorks());                 try {                     Thread.sleep(200);                 } catch (InterruptedException e) {                     e.printStackTrace();                 }             }         }     }).start();}
Link to comment
Share on other sites

Link to post
Share on other sites

My guess is that dbc isn't a global variable. You're inside a function and you never pass it dbc so that would be my assumption.

 

You say it's a function in another class. Either dbc needs to be a class with a public method connectionWorks() that the function can access or dbc needs to be the instance of that class, and in this case it would have to be global for the function to see it. It would help if you posted more of the programming or provided some more details.

 

Source: I do this everyday.

Turnip OC'd to 3Hz on air

Link to comment
Share on other sites

Link to post
Share on other sites

magic happened, it works.
dbc is an instance of an object with a public boolean connectionWorks()

private void addDatabaseConnectionThread(){        t = new Thread() {            @[member=OverRide]            public void run() {                while (!interrupted() && threadRunning) {                    if (dbc.connectionWorks()){                        lblDBConnection.setText("");                    } else {                        lblDBConnection.setText("database connection failed");                    }                    try {                        Thread.sleep(3000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        };        t.start();    }

don't ask me why this works and the previeous one didn't.

Grammar nazis are people too!
Treat your local grammar nazi nicely and he might teach you a thing or two. (Note that I'm Belgian and not a native English speaker.)
Chivalry isn't dead!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×