trying to set a member variable in an object by running a thread

L

lbrtchx

Hi *,

I need to set a member variable in an object, by running a thread.

I think something like that should be doable, because this is how
GUI work right? There is something however I am not getting right here.

With this simple code example I just try to set the Dates of a number
of days from now using a DateFormatter and a given Local.

What is it I am not getting right here?

// __ CODE EXAMPLE
import java.util.*;
import java.text.*;

// __
class Runnbl00 implements Runnable{
private String[] aTimedDays;
private int iDays;
// __
private DateFormat frmttr;
// __
public Runnbl00(int iDays, String aPattern, Locale Lcl){
this.iDays = 1;
if((iDays > 0) && (iDays < 366)){ this.iDays = iDays; }
// __
frmttr = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Lcl);
}
// __
public void run(){
this.aTimedDays = new String[this.iDays];
long lTm = System.currentTimeMillis();
// __
for(int i = 0; (i < this.iDays); ++i){
this.aTimedDays = frmttr.format(new Date(lTm));
System.err.println("// __ in run(): " + aTimedDays);
lTm += 24*60*60*1000;
}// [0, this.iDays)
}
// __
public String[] getTimedDays(){ return(aTimedDays); }
// __
public int getDays(){ return(iDays); }
}

// __
public class TestRunnbl00{
// __
public static void main(String[] aArgs){
int iDays = 7;
String aPattern = "yyyyMMddHHmmss";
Locale Lcl = Locale.FRANCE;
Lcl = new Locale("en","US");
// __
Runnbl00 R = new Runnbl00(iDays, aPattern, Lcl);
(new Thread(R)).start();
String[] aTimedDays = R.getTimedDays();
// __
if((aTimedDays != null) && (aTimedDays.length > 0)){
for(int i = 0; (i < aTimedDays.length); ++i){
System.err.println("// __ in calling object: " + aTimedDays);
}// i [0, aTimedDays.length)
}
else{ System.err.println("// __ in calling object: " + aTimedDays); }
// __
System.err.println("// __ R.getDays(): " + R.getDays());
}
}

Albretch Mueller (lbrtchx)
 
B

BobSmith

Maybe this will help you:

Try a inner Thread like this

class MyClass {
int anInteger = 0;
...
//This method will create a thread to do
//its action, and then return.

public void aMethod() {

new Thread() {
public void run() {
while(anInteger < 60) {
try {
Thread.sleep(100);
System.out.println(anInteger);
anInteger += 1;
}
catch (Exception e) {e.printStackTrace();}
} //end while
} //end run()
}.start(); //end Thread()

} //end aMethod
...
} //end MyClass

I'm sure what you are doing works too, but this is what I like to use,
and I have difficulty reading your code because of the indenting.
 
P

Patricia Shanahan

Runnbl00 R = new Runnbl00(iDays, aPattern, Lcl);
(new Thread(R)).start();
String[] aTimedDays = R.getTimedDays();
....

You look at the result, in the main thread, without having done anything
to ensure that the spawned thread has finished its work.

In theory, that leaves the relative ordering between the spawned thread
and main thread operations undefined. In practice, any reasonably sane
operating system is going to carry on with the thread it is already
running to see if it finishes or has to wait, rather than instantly
context switching to the newly created thread

You could replace your start() call line with:

Thread other = new Thread(R);
other.start();
other.join();

That is a bit pointless, but you could insert other main thread work
between other.start() and other.join().

Patricia
 
J

jan V

What is it I am not getting right here?

Sooner or later this will bite you (or the team you work in): your choice of
identifiers sucks big time.

Want to know why? Go read "Code Complete" by McConnell.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top