Computing.Net > Forums > Programming > Java thread problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Java thread problem

Reply to Message Icon

Name: Spinal
Date: February 19, 2006 at 09:18:45 Pacific
OS: JDK1.4 / eclipse
CPU/Ram: enough
Comment:

Hello! I'm trying to do some work in java, but am having some serious difficulties. This is the problem, I have my main program in a class, say class A. This class creates a thread, class B. This thread does nothing more than update a variable. My problem is that the variable needs to be read by class A, which needs to display that variable in a JTextField. I would post source code, but its fairly large and ugly :P so here is a uqick extract: (any ideas?) (really this is just a counter example, but I have no idea how to sync the two instances of my "counter", i.e. the one in class A that can display it, and the one in class B that updates it)

[code]

class A
{
static Mat mat;
mat.init();
Thread matTransmit = new MatSender (mat);
matTransmit.start();
JTextField myText;
myText.setText(mat.stateList);
}

public class MatSender extends Thread{
private Mat mat;

MatSender(Mat m){
this.mat =m;
}

public void run(){
while (true){
/*
* Non critical section
*/
cs();
}

}
private void cs(){
// critical section
String matData = mat.stateList();
matBuffer.setText(matData);

}
}

package virtua;

public class Mat {
boolean[][] mat = new boolean[9][9];

void init (){
for (int i=0; i<9; i++){
for (int j=0; j<9; j++){
mat[i][j] = false;
}
}
}

void pressAt(int x, int y){
mat[x][y] = true;
}
void releaseAt(int x, int y){
mat[x][y] = false;
}

boolean getState(int x, int y){
return mat[x][y];
}

String stateList(){
String matState = "{";

for (int i=0; i<9; i++){
for (int j=0; j<9; j++){
if (mat[i][j])
{
matState += "(" +(i+1)+ ", " +(j+1)+ ")";
}

}
}
matState += "}";
return matState;
}
}
[/code]



Sponsored Link
Ads by Google

Response Number 1
Name: Spinal
Date: February 19, 2006 at 09:33:59 Pacific
Reply:

Just realised, I didn't explain myself correctly! Simplified:
I have 2 classes, one which creates a GUI and one with needs to update a single element within that GUI every x seconds.

Im implementing this as a thread, so the GUI is created, and a thread is created to the updateGUI class. This now runs on its own while the user does his business, and updates the JTextField every x seconds.

Ideas?


0

Response Number 2
Name: Spinal
Date: February 19, 2006 at 09:46:44 Pacific
Reply:

I think I got it! Thru random experimentation, this seems to work:
A.matBuffer.setText(mat.stateList());

(where A is my main class)

Just thought i'de post this for posterity :P


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Java thread problem

Java Run Problem www.computing.net/answers/programming/java-run-problem/5044.html

Java Threads in Win2k www.computing.net/answers/programming/java-threads-in-win2k/6009.html

java threads programming www.computing.net/answers/programming/java-threads-programming/12762.html