As you shall see, the real problem with concurrency occurs when tasks that are executing in parallel begin to interfere with each other.
If you ignore it, you're likely be get bitten.
The many faces of concurrency:The problems that you solve with concurrency can be roughly classified as "speed" and "design manageability."
- Faster execution -If you want a program to run faster, break it into pieces and run each piece on a separate processor.
- Utilize multiple-precessor system
To define a task, simply implement Runnable and write a run( ) method to make the task do your bidding.
/**
* Display the countdown before liftoff.
* Demonstration of the Runnable interface
* @author WPeng
*
* @since 2012-11-19
*/
public class LiftOff implements Runnable{
protected int countDown = 10; //Default
private static int taskCount = 0;
private final int id = taskCount++;
public LiftOff(){}
public LiftOff(int countDown){
this.countDown = countDown;
}
public String status(){
return "#" + id + "(" + (countDown > 0 ? countDown : "LiftOff") + ") ,";
}
@Override
public void run() {
while(countDown-- >0){
System.out.println(status());
//I’ve done the important parts of my cycle
//and this would be a good time to switch to another task for a while.
Thread.yield();
}
}
public static void main(String[] args){
LiftOff launch = new LiftOff();
launch.run();
}
}
/**
#0(9) ,
#0(8) ,
#0(7) ,
#0(6) ,
#0(5) ,
#0(4) ,
#0(3) ,
#0(2) ,
#0(1) ,
#0(LiftOff) ,
*/
A Thread constructor only needs a Runnable objectCalling a Thread object’s start( ) will perform the necessary initialization for the thread and then call that Runnable’s run( ) method to start the task in the new thread.
/**
* The most basic use of Thread class
* @author WPeng
*
* @since 2012-11-20
*/
public class BasicThreads {
public static void main(String[] args) {
Thread t = new Thread(new LiftOff());
t.start();
System.out.println("Waiting for LiftOff");
}
}
/**
* Waiting for LiftOff
* #0(9) ,#0(8) ,#0(7) ,#0(6) ,#0(5) ,#0(4) ,#0(3) ,#0(2) ,#0(1) ,#0(LiftOff) ,
* */
Executors are the preferred method for starting tasks in Java SE5/6.
We can use an Executor instead of explicitly creating Thread objects.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CashedThreadPool {
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
for(int i=0; i<5; i++)
exec.execute(new LiftOff());
exec.shutdown();
}
}
Very often, a single Executor can be used to create and manage all the tasks in your system.
The current thread (in this case, the one driving main( )) will continue to run all tasks submitted before shutdown( ) was called. The program will exit as soon as all the tasks in the Executor finish.
Note that in any of the thread pools, existing threads are automatically reused when possible.
A Runnable is a separate task that performs work, but it doesn’t return a value. If you want the task to produce a value when it’s done, you can implement the Callable interface rather than the Runnable interface.
class TaskWithResult implements Callable<String>{
private int id;
public TaskWithResult(int id){
this.id = id;
}
@Override
public String call() {
return "result of TaskWithResult " + id;
}
}
public class CallableDemo {
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
ArrayList<Future<String>> results = new ArrayList<Future<String>>();
for(int i=0; i<10; i++)
//with a type parameter representing the return value from the method call( )
//(instead of run( )), and must be invoked using an ExecutorService submit( ) method.
results.add(exec.submit(new TaskWithResult(i)));
for(Future<String> fs : results){
try {
// get() blocks until completion:
System.out.println(fs.get());
} catch (Exception e) {
// TODO: handle exception
} finally{
exec.shutdown();
}
}
}
}