java tutorial 8, main & scheduler threads questions

    Apr 27 2010 | 12:37 pm
    Coming back to Tutorial08.html, it says :
    It is important to understand that it is possible for a thread to be interrupted at any time by another thread. (...) This can lead to some unstable situations. Consider the following class:
    public class threadProblem extends MaxObject {
    private int[] data = new int[10];
    public void bang() {
    int size = data.length;
    for (int i=0;i size;i++) {
    data[i] = i;
    public void inlet(int i) {
    data = new int[i];
    An int sent in the object's inlet will create a new array of the given length, and a bang will populate the array for numbers. However, consider what would happen if a bang in the main thread (caused by user input, say) was interrupted sometime while executing the bang method and in the scheduler thread a integer was input? If the input is smaller than the previous size of the array, an exception will be thrown shortly after the flow of execution has passed back to the main thread when the for loop exceeds the end of the new array length. (...)
    If i understand well, a [delay 0] after any bang sent to the class would solve the problem in this case, am I right ?
    In some others situations, where i'll need to pass numbers and lists, will [pipe 0] act the same way - "moving" the data from main to scheduler thread - than [delay 0] ?
    i think I understood that if i want to sent critical timing events from java to max, i will need to use outletHigh instead of the outlet methods... and i'm then wondering... on the other way, if a kind of "inletHigh" feature (maybe thought a kind of declareInletHigh(number) method) would have been useful then in the above situation, as an alternative to the delay/pipe stuff ?
    Any comment or answer welcome,

    • Apr 27 2010 | 10:06 pm
      I would use defer or deferlow rather than delay or pipe.
      Not sure if using a {synchronized} block in Java would get around some of these issues, but it would certainly introduce some overhead with respect to object locking/unlocking, which would likely defeat the purpose if you're in a critical timing situation.
    • Apr 28 2010 | 11:20 am
      Thanks for your answer...
      >> I would use defer or deferlow rather than delay or pipe.
      I feel you might have missed something : i have read that [delay 0] is "moving" the data from main thread to scheduler thread (higher priority thread), while defer & deferlow are doing in fact the opposite...
      (Me, I wanted to know if [pipe] act the same way than [delay], "moving" the data to the higher priority thread, the scheduler thread.)
      >> a {synchronized} block in Java
      Sorry, i'm might not be a java expert, i'm not sure what you're talking about...