Monthly Challenge - #1 Januari 2015 - Thread Threat

Monthly Challenge - #1 Januari 2015 - Thread Threat


Basic Rules:

-Submit your result before februari in this topic with one or more screenshots (Please use the forum image uploader).

-No libraries unless stated otherwise.

-You have to create it yourself, this means not uploading someone else his code cause it fits the challenge. However, you can be inspired by somone else his code.

-You have to share your code so others can see how it works.

-If it uses data input then you have to share the data as well (please keep it small).

-the change of crashing should be minimal.


Basic Tips:

-This is processing, so instead of "hello world" it's hello visual output. So try yo make it visual appealing.

-There is no code limit, but this does not mean 5000 lines of code is a good idea. Others might want to learn from your code. It might be much more helpfull to do something really cool in a pretty basic sketch. Less is more. However, if you have a cool idea, do whatever it takes.

-Post videos (and screenshots) if it has motion.


#1 Januari 2015 - Thread Threat

For the first edition the idea is to loose control by using a thread. We start the first one a bit earlier with being having some days of and it being the first one. More about a thread can be readed here:

https://www.processing.org/reference/thread_.html

Here is another example of getting a thread running:

void setup() {
  thread("runForrestRun");
}

void draw() {

}

void runForrestRun() {
  println("run forrest run");
  delay(10);
  runForrestRun(); // recursive
}

Using java's way of running a thread is allowed. More then one thread is allowed. Even encouraged if it has a direct influence on the outcome. The way you loose contol should be in such a way that using a thread makes sense... or no sense at all :) I hope you get the idea. And I hope to see some clever results soon.

Best of Luck!

Comments

  • edited December 2014

    I never create a thread in my whole life, it's exciting ! :D

  • but I have a vague idea what is it....

    Zwischenablage01

  • Hey, i'm kind of new to processing, i came across this looking for challenges in processing and i don't understand what i'm meant to do here. please could someone explain.

  • hm... this is just a task you can do for fun...

    the task that month is to use threads / different cores of your computers CPU....

    so it's about multithreading / multitasking...

    it's relatively advanced actually, so maybe you want to try something else....

    you could try tic tac toe or so...

    ;-)

  • Here is my first submission:

    int x, y;
    void setup() {
      size(600, 600);
      thread("runForrestRun");
    }
    void draw() {
      for (x = 0; x < width; x++) {
        for (y = 0; y < height; y++) {
          set(x, y, color(map(x, 0, width, 0, 255), map(y, 0, height, 0, 255), x+y));
        }
      }
    }
    void runForrestRun() {
      while (true) {
        colorMode(RGB, x, y, width+height);
        delay((int)map(mouseX, 0, width, 1, 100));
      }
    }
    
  • Keep them coming! :))

  • edited January 2015

    Interesting challenge. I'll bite. Here's a program that uses threading to create static:

    boolean black = true;
    
    void setup(){
    
      size(500, 500);
    
      new Thread(){
       public void run(){
        while(true){
         black = !black;
        }
       } 
      }.start();
    }
    
    void draw(){
    
      for(int x = 0; x < width; x++){
        for(int y = 0; y < height; y++){
          if(black){
            stroke(0);
          }
          else{
            stroke(255);
          }
          point(x, y);
        }
      }
    }
    

    static

  • Hello !

    Can someone explain exactly what is possible to do with Threads because it's very unclear for me (never used a Thread before).

    I thought it was possible to share only a String between 2 Thread, but here you share a boolean...

    What is possible ? what is not possible ?

    Thank by advance !

  • Nice KevinWorkman. About your if else, you can also use this:

    stroke(black ? 0 : 255);

    @tlecoz It's a bit hard to explain. Running a Thread is a bit like running 2 sketches at the same time. The good thing is that if one runs really slow, the other is still responsive. However, this topic won't be good for learning about threads.

  • edited January 2015
    • Java as a language doesn't artificially makes concurrency safe. It's pretty raw! >-)
    • Thus concurrency is the most difficult feature for risk evaluation!
    • Most dangerous thing is writing. Although reading can be surprisingly problematic sometimes.
    • But the utmost offender is changing the size of a data structure.
    • If another Thread is traversing that data structure while its size is being changed elsewhere,
      either yields wrong output or outright crash! :-SS
  • Nice KevinWorkman. About your if else, you can also use this:

    Ha, yeah, I know. But if I was coding like a sane person, I wouldn't be using threads like this! And where would the fun be in that?

  • Can someone explain exactly what is possible to do with Threads because it's very unclear for me (never used a Thread before).

    I thought it was possible to share only a String between 2 Thread, but here you share a boolean...

    What is possible ? what is not possible ?

    Think about it this way: without threads, the computer can only do one thing at a time. So if you push a button that loads a large file, you wouldn't be able to show a progress bar until after the file was loaded- and that wouldn't be very useful!

    An example of threading is using another thread to load that file, while you show the progress bar on your first thread. The computer looks like it's doing two things at once (showing the progress bar and loading the file), but in reality it's just switching between two threads.

    Threading is a pretty complicated subject, so if you're still learning the basics, you shouldn't worry too much about it. But to answer your question, you can definitely share more than a String between threads!

    If you really want more info, I'd recommend googling "java concurrency tutorial", but like I said, I wouldn't worry too much about it.

    Also, the sketches in this thread are just for fun, and are probably not good examples of how to use threads.

  • edited January 2015

    Threads: When you convert a movie e.g., one thread can do one part, another thread another. Each processor or each core can do one part. Much faster than only one processer / processor core do it.

    Even calculating big images, different parts can be done in different threads.

    Also Grapic Cards now a days are multi core / multi thread.

    Also in Browsers, some have one thread for each tab. Or in chess programs when calculating a move and the depth is very high (looking at future moves).

    It gets complicated when e.g. in a database, threads want to change the same entries or one thread has to wait for the results on another thread.

    http://en.wikipedia.org/wiki/Thread_(computing)

    ;-)

  • edited January 2015

    Here is the result of my effort with multiple threads.

    and the code

    SharedImage si;
    
    void setup() {
      size(200, 200);
      background(0);
      si = new SharedImage(get());
    
      colorMode(HSB, 1.0);
      Scribbler sc;
      // Will create 'nt' threads
      int nt = 11;
      float deltaH = 1.0 / nt;
      for (float h = 0; h < 1.0; h += deltaH) {
        sc = new Scribbler(si, color(h, 0.8, 1));
        sc.start();
      }
    }
    
    void draw() {
      background(si.img);
    }
    
    void mouseClicked() {
      si.img.save("pipes" + millis() + ".png");
    }
    
    class Lock {
      protected boolean locked;
    
      public Lock() {
        locked = false;
      }
    
      synchronized void lock() {
        while (locked)
        try {
          wait();
        } 
        catch (InterruptedException e) {
          e.printStackTrace();
        }
        locked = true;
      }
    
      synchronized void unlock() {
        locked = false;
        notify();
      }
    }
    
    class SharedImage {
    
      PImage img;
      int w, h;
    
      Lock lock = new Lock();
    
      SharedImage(PImage image) {
        img = image;
        w = img.width;
        h = img.height;
      }
    
      void update() {
        lock.lock();
      }
    
      void done() {
        lock.unlock();
      }
    }
    
    class Scribbler extends Thread {
    
      SharedImage sharedImage;
      int col;
      int x, y, dir;
    
      Scribbler(SharedImage image, int scol) {
        sharedImage = image;
        col = scol;
        x = (int) random(0, sharedImage.w);
        y = (int) random(0, sharedImage.h);
        dir = (int) random(0, 4);
      }
    
      void run() {
        while (true) {
          sharedImage.update();
          // Change direction
          switch((int)random(0, 20)) {
          case 0:
            dir += 1;
            break;
          case 1:
            dir -= 1;
            break;
          }
          dir = (dir + 4) % 4;
          // Next point
          switch(dir) {
          case 0:
            y -= 1;
            break;
          case 1:
            x += 1;
            break;
          case 2:
            y += 1;
            break;
          case 3:
            x -= 1;
            break;
          }
          // Wrap round screen
          x = (x + sharedImage.w) % sharedImage.w;
          y = (y + sharedImage.h) % sharedImage.h;
          sharedImage.img.set(x, y, col);
          sharedImage.done();
          // Give the other scribblers a chance
          try {
            sleep(50);
          } 
          catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }
    
  • nice Quark. I was hoping for someone to play with changing xy values :)

  • Here is another effort with a bit more oomph! I call it Threaded Worms

    lines4660

    Worm[] worms;
    int[] colours;
    
    void setup() {
      size(400, 400);
      strokeWeight(2);
      colours = new int[] { 
        color(255, 0, 0), color(0, 255, 0), color(0, 0, 255), color(255, 255, 0), color(0, 255, 255), color(255, 0, 255), color(0), color(230)
      };
      worms = new Worm[colours.length];
      for (int i = 0; i < worms.length; i++) {
        worms[i] = new Worm(width, height, colours[i], random(0.04, 0.09));
        worms[i].start();
      }
    //  int newPrio = Thread.currentThread().getPriority() + 1;
    //  Thread.currentThread().setPriority(newPrio);
    }
    
    synchronized void draw() {
      background(80);
      for (Worm w : worms)
        w.display();
    }
    
    void mouseClicked() {
      save("lines" + millis() + ".png");
    }
    
    class Worm extends Thread {
    
      protected boolean locked = false;
    
      synchronized void lock() {
        while (locked)
        try {
          wait();
        } 
        catch (InterruptedException e) {
          e.printStackTrace();
        }
        locked = true;
      }
    
      synchronized void unlock() {
        locked = false;
        notify();
      }
    
      PVector start, end, head, tail;
      float tHead, tTail;
      float dx, dy, dt, ds;
      int col;
      float w, h;
      float lastTime, currTime;
    
      Worm(int w, int h, int col, float dt) {
        this.w = w;
        this.h = h;
        this.col = col;
        this.dt = dt;
        ds = 4.5f;
        end = new PVector(random(w), random(h));
        start = new PVector();
        head = new PVector();
        tail = new PVector();
        getNextEnd(start, end);
      }
    
      void run() {
        while (true) {
          lock();
          updatePos();
          unlock();
          try {
            sleep(40);
          } 
          catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    
      void display() {
        lock();
        fill(col);
        stroke(col);
        line(tail.x, tail.y, head.x, head.y);
        ellipse(start.x, start.y, ds, ds);
        ellipse(end.x, end.y, ds, ds);
        unlock();
      }
    
      void getNextEnd(PVector s, PVector e) {
        float d;
        s.set(e);
        do {
          e.set(random(w), random(h));
          d = PVector.dist(s, e);
        } 
        while (d < 50 || d > 200);
        head.set(s);
        tail.set(s);
        tHead = 0;
        tTail = -0.25f;
        dx = e.x - s.x;
        dy = e.y - s.y;
      }
    
      void updatePos() {
        tHead += dt;
        update(head, tHead);
        tTail += dt;
        update(tail, tTail);
        if (tTail >= 1)
          getNextEnd(start, end);
      }
    
      void update(PVector v, float t) {
        if (t <= 0)
          v.set(start);
        else if (t >= 1.0)
          v.set(end);
        else {
          v.x = start.x + dx * t;
          v.y = start.y + dy * t;
        }
      }
    }
    
  • In what way does the Thread influence the result?

  • edited January 2015

    i love threaded worms!

    worms1

  • If you want to add more worms simply add a colour to the array in line 8 :D

  • Is there going to be a next challenge?

  • yup. Just posted.

    http://forum.processing.org/two/discussion/9272/monthly-challenge-2-februari-2015-console-canvas

    If people wan't they can still post for this challenge. You won't get banned :)

Sign In or Register to comment.