threading/deferLow question


    Feb 01 2006 | 9:54 pm
    Hi all,
    I'm pretty new to java. I know that this is a pretty basic question,
    but I would appreciate some advice.
    I'm working on a buffer editing program, borrowing heavily from buf.Op.
    There are two main buffer-accessing functions: one which clears the
    entire buffer: clear(), and one which draws my curve into the buffer:
    draw(). As it stands, the buffer-accessing parts of both of these
    functions are within deferLow executables.
    Every time draw() is called, I'd like to clear the buffer first. But
    I'm hesitant to call clear() (which is deferred) and then draw()
    function (which is also deferred) because the clear function needs to
    finish before the draw function even starts.
    My intuition tells me that if I make a clear_nondeferred() which isn't
    deferred, and call it from my deferred executable in draw(), then I'll
    be safe and not jam up my high-priority thread.
    True?
    thanks for your help,
    jim

    • Feb 01 2006 | 10:01 pm
      jim.
      i am having a hard time figuring out exactly what you are
      talking about. do you have any code you could post?
      it might give me more of a context and make it easier
      for me to have some insight.
      topher
    • Feb 01 2006 | 10:45 pm
      sorry, i was being lazy not wanting to cut down my code into a readable bit.
      so, basically, the deal is i would like to be able to call the update() method from max, first have it clear the buffer, then do its stuff. but i would like this to all be low priority, so i don't hang up max while clearing/writing the buffer.
      the question is: should i use clearbuf() or clearbufnonthreaded()?
      public void clearbuf()
      {
      MaxSystem.deferLow(new Executable()
      {
      public void execute()
      {
      for (long i = 0; i < buflength; i++)
      {
      MSPBuffer.poke(bufname, 1, i, 0);
      }
      }
      }
      );
      }
      public void clearbufnonthreaded()
      {
      for (long i = 0; i < buflength; i++)
      {
      MSPBuffer.poke(bufname, 1, i, 0);
      }
      }
      public void update()
      {
      MaxSystem.deferLow(new Executable()
      {
      public void execute()
      {
      clearbufnonthreaded(); //or should i call the clearbuf() function?
      Iterator it = bands.iterator();
      while (it.hasNext())
      {
      // calculate stuff
      for (long i = colowbin; ((i {
      MSPBuffer.poke(bufname, 1, i, 1);
      }
      }
      }
      }
      );
      }
    • Feb 02 2006 | 12:39 am
      >the question is: should i use clearbuf() or clearbufnonthreaded()?
      I would say do the latter. You could also do something like this if
      you wanted to just use java threading:
      > private boolean _running = false;
      > public void clearbuf()
      > {
      > MaxSystem.deferLow(new Executable()
      > {
      > public void execute()
      > {
      > _clearbuf();
      > }
      > }
      > );
      > }
      >
      > private void _clearbuf()
      > {
      > for (long i = 0; i < buflength; i++)
      > {
      > MSPBuffer.poke(bufname, 1, i, 0);
      > }
      > }
      >
      > public void update()
      > {
      if(_running)
      return;
      > Thread t = new Thread()
      > {
      > public void run()
      {
      > _running = true;
      > _clearbuf();
      > Iterator it = bands.iterator();
      > while (it.hasNext())
      > {
      > // calculate stuff
      > for (long i = colowbin; ((i
      > > {
      >
      > MSPBuffer.poke(bufname, 1, i, 1);
      > }
      > }
      _running = false;
      > }
      > }
      > );
      t.start();
      > }
      >
      >sorry, i was being lazy not wanting to cut down my code into a readable bit.
      >
      >so, basically, the deal is i would like to be able to call the
      >update() method from max, first have it clear the buffer, then do
      >its stuff. but i would like this to all be low priority, so i don't
      >hang up max while clearing/writing the buffer.
      >
      >
      >
      >
      > public void clearbuf()
      > {
      > MaxSystem.deferLow(new Executable()
      > {
      > public void execute()
      > {
      > for (long i = 0; i < buflength; i++)
      > {
      > MSPBuffer.poke(bufname, 1, i, 0);
      > }
      > }
      > }
      > );
      > }
      >
      > public void clearbufnonthreaded()
      > {
      > for (long i = 0; i < buflength; i++)
      > {
      > MSPBuffer.poke(bufname, 1, i, 0);
      > }
      > }
      >
      > public void update()
      > {
      >
      > MaxSystem.deferLow(new Executable()
      > {
      > public void execute()
      > {
      > clearbufnonthreaded(); //or should i
      >call the clearbuf() function?
      >
      >
      > Iterator it = bands.iterator();
      > while (it.hasNext())
      > {
      > // calculate stuff
      > for (long i = colowbin; ((i
      > > {
      >
      > MSPBuffer.poke(bufname, 1, i, 1);
      > }
      > }
      > }
      > }
      > );
      > }
      >
    • Feb 02 2006 | 2:07 am
      thanks, topher. i think the threading choice makes more sense, anyway. thanks for the example.
      -jim
      Quote: topher lafata wrote on Wed, 01 February 2006 17:39
      ----------------------------------------------------
      >
      > >the question is: should i use clearbuf() or clearbufnonthreaded()?
      >
      > I would say do the latter. You could also do something like this if
      > you wanted to just use java threading:
      [...snip]