Java ProcessBuilder examples

In Java, we can use ProcessBuilder to call external commands easily : ProcessBuilder processBuilder = new ProcessBuilder(); // — Linux — // Run a shell command processBuilder.command("bash", "-c", "ls /home/mkyong/"); // Run a shell script processBuilder.command("path/to/hello.sh"); // — Windows — // Run a command processBuilder.command("cmd.exe", "/c", "dir C:\\Users\\mkyong"); // Run a bat file processBuilder.command("C:\\Users\\mkyong\\hello.bat"); Process …

Read more

Java BlockingQueue examples

In Java, we can use BlockingQueue to create a queue which shared by both producer and the consumer. Producer – Generate data and put it into the queue. Consumer – Remove the data from the queue. Note Read this article to understand what is producer and consumer. The BlockingQueue implementations are thread-safe, safely be used …

Read more

Java Semaphore examples

In Java, we can use Semaphore to limit the number of threads to access a certain resource. 1. What is Semaphore? In short, a semaphore maintains a set of permits (tickets), each acquire() will take a permit (ticket) from semaphore, each release() will return back the permit (ticket) back to the semaphore. If permits (tickets) …

Read more

Review : Java 7 Concurrency Cookbook

The Java 7 Concurrency Cookbook, containing over 60 examples show you how to do multithreaded programming in Java. It shows varies threading topics from beginner level to advanced level, including thread management like create, interrupt and monitor thread, using Java 5 Executor frameworks to run or schedule threads, and the latest Java 7 fork/Join Frameworks …

Read more

Spring and Java Thread example

Here are 3 examples to show you how to do “threading” in Spring. See the code for self-explanatory. 1. Spring + Java Threads example Create a simple Java thread by extending Thread, and managed by Spring’s container via @Component. The bean scope must be “prototype“, so that each request will return a new instance, to …

Read more