need help in locking or synchronization
Dear All,
I have a code fragment that is not working. I posted it before on this same forum, but now the problem is better defined, and the code is changed a bit.
I am having event dispatch thread trouble again, the thing is that when i run the program by clicking the 1-step button, it executes perfectly, but when i press start, then half the total no of grids do no get repainted. The other half does, I'm at my wits end at how to solve this problem.
I have pasted the code below, I know it's long, but could someone please take a look at it?
There is a save file that you need to load, to make it run. I would be more than happy to provide it and the remaining code if required.
The problem lies in the part somewhere between when I call the update function and the repaint function in the first code. The problem of half the array not showing gets sorted out if i use the synchronized keyword to equate the arrays, but then the results aren't correct, as the arrays tend to get equated the moment i put a new value in buffer[][]. I think I need to somehow lock the program so that it doesn't execute further till the update function is complete. The code is correct, but not robust, as it executes much better in a friend's laptop.
I will much appreciate any help. Thanks.
Problematic Code fragment:
Code:
while( true ){
if( running ){
startTime = System.currentTimeMillis();
exchanges.update();
elapsedTime = System.currentTimeMillis() - startTime;
waitTime = Math.max( delayTime - elapsedTime, 5 );
try{
Thread.sleep(waitTime);
}catch (InterruptedException ie) {}
da.repaint();
}
Paint Component code:
Code:
public void paintComponent( Graphics g ){ // main rendering recipe
Graphics2D g2 = (Graphics2D)g;
g2.setColor( Color.black );
g2.fillRect( 0, 0, getWidth()-1, getHeight()-1 );
int w = DISP_WIDTH / exchanges.width;
int h = DISP_HEIGHT /exchanges.height;
for(int col=0;col<exchanges.width;col++){
for(int row=0;row<exchanges.height;row++){
for(int k=0; k<11; k++){ //replaced with works1.l
if( exchanges.cells[col][row] == k ){
if( exchanges.buffer[col][row] == exchanges.cells[col][row] )
g2.setColor( exchanges.colarray[k] ); // is and was a cooperator
}
}
g2.fillRect( col * w +1, row * h +1, w-2, h-2 );
}
}
}
Update Code:
Code:
public void update(){
int maxValue = set[0][0];
for(int i=1;i < set.length;i++){
if(set[i][0] > maxValue){
maxValue = set[i][0];
}
}
int minValue = set[0][0];
for(int i=1;i<set.length;i++){
if(set[i][0] < minValue){
minValue = set[i][0];
}
}
for(int col=0; col<width; col++) {
for( int row=0; row<height; row++) {
int count = 0;
if( ( (col-13+set[0][1]) >= 0 ) && ((row-10+minValue) >= 0) && ( (col-13+set[set.length-1][1]) < width) && (row-10+maxValue)<height ) {
for( int r=0; r<rules.length; r++) {
if (cells[col][row] == child [r][0][0]) {
for( int i=0; i<set.length; i++) {
if( rules[r][set[i][1]][set[i][0]] == cells [col-13+set[i][1]][row-10+set[i][0]] )
{ count++; }
}
if(count == set.length) {buffer[col][row] = child[r][1][0];}
count = 0;
}
}
}
}
}
for (int i=0; i<width; i++) {
for(int j=0; j<height; j++) {
cells [i][j] = buffer [i][j]; // new = old
}
}
}