2

I'm using StyledText 400x100 widget and it's working like a console where the program interacts with the user.

This is how I update the widget:

private static Shell MainShell = null;

public void createPartControl(Composite parent){
   MainShell = parent.getShell();
}

public static void updateConsole(final String newMessage){
   if(MainShell.isDisposed() || myStyledText.isDisposed() ) return;

   MainShell.getDisplay().syncExec(new Runnable(){
      myStyledText.setText( newMessage + "\n" + myStyledText.getText() );
   });
}

Similar to append(), but this one insert to the first line and inserts a line break "\n".

I'm using CycleBarrier to handle Threads. Currently it's running 300+ threads, and I'm allowing only 10 threads / cycle to not kill the CPU.

// divide 300/10 because there is an inner for() which runs 10 threads / cycle
for(int n = 0; n < 300/10; n++){

   // allow only 10 threads to work
   final CycleBarrier br = new CycleBarrier(10);

   for(int i = 0; i < 10; i++){
      new Thread(new MyClass(cb).start();
   }

   //waiting for Threads to reach the barrier
   br.await();
}

And now the MyClass class:

public MyClass implements Runnable{
   private CycleBarrier cb;

   public MyClass(CycleBarrier cb){
      this.cb = cb;
   }

   @Override
   public void run(){
      for(int i = 0; i < 256; i++){
         for(int j = 0; j < 256; j++){
            //View is the main class (eclipse RCP) and updateing the widget
            View.updateConsole("matrix["+i+"]["+j+"]");

            // Just an integer which counts the number of the loops
            View.TOTAL_LOOPS++;
         }
      }
      cb.await();
   }
}

This was an example. It should write to the View widget in asynchronous ways (not in order) because the Threads doesn't reaches the barrier in order.

I'm using eclipse RCP (3.8).

ISSUE

Why the program is working correct in DEBUG mode? I have set a breakpoint where I'm starting new Threads (in the inner for() ) and I'm clicking the Resume button to start threads one by one. When I'm trying to open in normal mode (RUN or exported) there are "leaks" (I don't know how to name), there are less lines in the console. View.TOTAL_LOOPS Should have in total:

256*256*10*30 = 19660800 // View.TOTAL_LOOPS++; in the MyClass

and in normal run it's having dynamic results: 174614904, 17025759, etc. In DEBUG mode it's reaching the exact value.

Question:

Are the Threads being killed?

Zbarcea Christian
  • 9,367
  • 22
  • 84
  • 137

1 Answers1

2

It has nothing to do with SWT. You are incrementing a single shared variable from 10 threads at once. This is a classic example of a race condition. Since ++ isn't an atomic operation, something like this can happen:

int temp = View.TOTAL_LOOPS; // in thread 1
int temp = View.TOTAL_LOOPS; // in thread 2
int temp2 = temp + 1; // in thread 1
View.TOTAL_LOOPS = temp2; // in thread 1
int temp2 = temp + 1; // in thread 2
View.TOTAL_LOOPS = temp2; // in thread 2

Note View.TOTAL_LOOPS only increases by 1 after this, and obviously it won't happen if you start threads one-by-one.

Use an AtomicInteger instead if you just want a thread-safe counter or otherwise synchronize your threads properly.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487