I am trying to understand J2ME code
Thread aaa = new Thread(pb) { private final ProgressBar val$pb;
public void run() { while (this.val$pb.getValue() < 100) {
try {
this.val$pb.setValue(this.val$pb.getValue() + 1);
Thread.sleep(10L); } catch (InterruptedException ex) {
}
this.val$pb.repaint();
}
mainForm.homeScreen = new HomeScreen();
mainForm.homeScreen.show();
}
};
aaa.start();
please tell me what does pb do in Thread constructor. How this code will look like if I Change new Thread(pb) to new Thread() ? Does it affect val$pb ? above code could not be compiled so I edited like this
Thread aaa = new Thread() { private final ProgressBar val=null ;//new Thread(pb) ProgressBar val$pb;
public void run() { try {while (this.val.getValue() < 100) { //try added by me
try {
this.val.setValue(this.val.getValue() + 1);
Thread.sleep(10L); } catch (InterruptedException ex) {
}
this.val.repaint(); //draws progress bar as a loading screen before showing home screen
}
}catch(Exception e){
mainForm.homeScreen = new HomeScreen();
mainForm.homeScreen.show(); // draws home screen
}
}
};
aaa.start();
EDIT:- it uses J4ME library.
If your code compiles then
pbis a parameter toThreadconstructor.Given that it is a single parameter, it may be either Runnable or String since Java ME API specifies only these objects possible as a single parameter in Thread constructor.
If pb is
Runnablethen things likely won’t change because Thread instanceaaaoverrides run method (which would be otherwise invoked for pb) and because in your code snippet, there are no traces of invokingpb.runanyway else (which smells of design error or of too much code cut from your snippet).If pb is
Stringthen name of the Threadaaawill be default instead of pb value.Hard to tell unless you post more code – preferably in SSCCE form.
val$pblooks funny but this could be a legal identifier for a variable assuming that in the code snippet you have cut something that initializes it.update in the second version of your code, given that you initialized
private final ProgressBar val=null– as a result, in the run method statements that invoke methods on it will throw NPEthis.val.getValue()and proceed right into catch block that draws home screen according to code comments.update2
Well with limited amount of code in your snippet one can only guess.
One way that comes to mind is to init with
pb, like this:above may compile if pb refers to ProgressBar instance and is declared final.
Note that in this case,
val$pbis not quite necessary becausepbcan be used instead of it (maybe this variable was introduced for code style preferences).Also, given your reference to j4me have to admit that using ProgressBar in thread constructor – if pb is an instance of this class – makes very little sense to me. One can only wonder how did it appear there in the initial snippet you posted
Thread aaa = new Thread(pb)...