ALL
Why can System.out.println be used to exit while loop
Let's first take a look at one simple Java thread code snippet which is supposed to exit the while loop after the first loop run.public class StopThread { private static boolean stopRequested; public static void main(String[] args) throws InterruptedException { Thread backgroundThread = new Thread(new Runnable() { @Override public void run() { int i = 0; while (!stopRequested) { i++; } } }); backgroundThread.start(); TimeUnit.SECONDS.sleep(1); stopRequested = true; }}But the truth is that the loop may not exit and keep running. The reason is that the updated stopRequested value will be in the ...
Concurrency vs Parallelism
Concurrency and parallelism are two related concepts which deal with executing tasks "simultaneously". From Sun's Multithreaded Programming Guide:Concurrency: A condition that exists when at least two threads are making progress. A more generalized form of parallelism that can include time-slicing as a form of virtual parallelism.Parallelism: A condition that arises when at least two threads are executing simultaneously.These two definitions are too formal to be understood easily. On the Internet, there is one image which demonstrates the difference between these two in an easy way:Much e...
29,556 0 CONCURRENCY THREAD PARALLELISM
Simple explanation about process and thread
Process and thread are two basic concepts of operating system, but they are a bit abstract which cannot be mastered easily.There is an analogy which explains these concepts very well.1. The kernel of the computer is CPU, it handles all the computing tasks, it's like a factory and will run all the time.2. Assume there is power limitation for the factory, it can only supply to one unit once,i.e, when one unit is working, other units must stop and wait. The meaning behind this is that each CPU can handle only one task at once.3. Process likes the unit in the factory, it represents the singl...
11,996 0 PROCESS OPERATING SYSTEM CPU THREAD
The several flavors of random in Java
Random number generation is one of most basic features in any programming language. The basic utilization is always the same: generate a random number between 0 and 1. With such a simple resource at hand we sometimes overlook some interesting features.What do we learn from the books?The most obvious and maybe intuitive way to generate random numbers in Java is simply calling:java.lang.Math.random()Random generation is in the Math utility class with abs, pow, floor, sqrt and other mathematical functions we see in all other languages. Most people will learn about this class...
The Erlang Design Pattern
Over the last couple of weeks I did an OO programming experiment. I call it the Erlang design pattern. It is based on the Actor model but goes some steps further. At its core just like the Actor model there are active entities (objects) that have a thread and a message queue with the thread waiting on the message queue to do some stuff.The Erlang design pattern extends the Actor model by first dividing the software program into active (actors, that have their own thread) and passive (objects, whose methods are called by other actors) objects.Second, I enforced that all methods of an ...
6,716 0 ERLANG THREAD PATTERN OS THREADS