Concurrency
Java Runtime.exec() Linux Pipe
If you want to utilize Linux pipe advantages, you can do it in Java. You may want to do the following, which is wrong. String cmd = "ls" Process p = Runtime.getRuntime().exec(cmd);String cmd = "ls" Process p = Runtime.getRuntime().exec(cmd); The correct way of executing shell command is the following: String[] cmd = { "/bin/sh", "-c", … Read more
Interview Question – Use Java Thread to Do Math Calculation
This is an example for showing how to use join(). Interview question: Use Java multi-threading to calculate the expression 1*2/(1+2). Solution: Use one thread to do addition, one thread to do multiplication, and a main thread to do the division. Since there is no need to communicate data between threads, so only need to consider … Read more
Monitors – The Basic Idea of Java Synchronization
If you took operating system course in college, you might remember that monitor is an important concept of synchronization in operating systems. It is also used in Java synchronization. This post uses an analogy to explain the basic idea of “monitor”. 1. What is a Monitor? A monitor can be considered as a building which … Read more
Java Thread: an overriding example code
class A implements Runnable { public void run() { System.out.println(Thread.currentThread().getName()); } } class B implements Runnable { public void run() { new A().run(); new Thread(new A(), "name_thread2").run(); new Thread(new A(), "name_thread3").start(); } } public class Main { public static void main(String[] args) { new Thread(new B(), "name_thread1").start(); } }class A implements … Read more
Java Thread: notify() and wait() examples
This article contains two code examples to demonstrate Java concurrency. They stand for very typical usage. By understanding them, you will have a better understanding about notify() and wait(). 1. Some background knowledge synchronized keyword is used for exclusive accessing. To make a method synchronized, simply add the synchronized keyword to its declaration. Then no … Read more