Is this async?

Discussion in 'Programming' started by Terrobility, May 15, 2016.

Thread Status:
Not open for further replies.
  1. Code (Java):
    import java.util.concurrent.atomic.AtomicLong;

    public class HelloWorld {
        public static void main(final String[] args) {

            final AtomicLong asyncCount = new AtomicLong();

            final Runnable printAndIncrement = new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        System.out.println("Hello world " + asyncCount.incrementAndGet());
                    }
                }
            };

            System.out.println("Starting new threads...");

            // Launch 2147483647 threads.
            for (int i = 0; i < 2147483647; i++) {
                System.out.println("Starting thread " + i);
                new Thread(printAndIncrement).start();
            }

        }
    }
    My Hello World programs come with a bit of a bang. And maybe a java.lang.OutOfMemoryError as well. Is this really async?
     
    • Funny Funny x 1
  2. It is not async when you are creating those threads.
     
  3. It is async, and obviously you're going to run out of memory doing that. Creating 2 billion threads that all have infinite loops? Yea
     
    • Agree Agree x 1
  4. Your computer can't handle 2 trillion threads, who knew? It actually hurts to look at this program.
     
    • Funny Funny x 1
  5. Sometimes I go on those online java IDE sites, paste this in and run it for fun.
     
    • Funny Funny x 1
    • Optimistic Optimistic x 1
  6. Well, it'll just time out after a little bit and stop running.
     
  7. md_5

    Administrator Developer

    Is this a sink?
    [​IMG]
     
    • Funny Funny x 10
    • Optimistic Optimistic x 1
  8. It appears it is, there's a spigot
     
  9. Difficult question. I would say yes, but some people might think it's not.
     
  10. I love how this is literally documented and you were still confused as to what the problem was.
     
  11. Why not just
    Code (Text):
    public void run() {
      new Thread() {
        run {
          while(true) {
              run();
          }
        }
      }
    }
     
    • Winner Winner x 1
Thread Status:
Not open for further replies.