java 笔记 : 多线程处理–runnable & callable
1. runnable interface – this interface could be called by Thread() or ExecutorService
import static org.junit.Assert.*;
import java.util.Random;
import org.junit.Test;
public class Test_runnable {
@Test
public void test() throws InterruptedException {
System.out.println("Begin Test!");
int nThreadCnt=15;
Thread[] threads = new Thread[nThreadCnt];
for (int i=0;i<nThreadCnt;i++) {
Thread t = new Thread(new Job(i));
threads[i] = t;
//if here use t.run(), then it will be
//synchronised call, the main thread will
//wait till the thread end, so here we should
//use t.start();
t.start();
}
Boolean bAllComplete=false;
while(!bAllComplete) {
bAllComplete = true;
for (int i=0;i<nThreadCnt;i++) {
if (threads[i].isAlive()) {
bAllComplete = false;
Thread.yield();
break;
}
}
}
System.out.println("End Test!");
}
}
class Job implements Runnable {
@Override
public void run() {
System.out.println("Begin job " + this.id);
Thread.yield();
for (int i=0;i<seed;i++) {
System.out.printf("Job id=%d, seed= %d of %d\n"
, this.id, i+1, this.seed);
Thread.yield();//give other thread opportunities to run
}
System.out.println("End job " + this.id);
}
public Job(int id) {
this.id = id;
}
private int id ;
private int seed =Math.abs((new Random()).nextInt()) % 10;
}
2. callable interface – callable must use ExecutorService to run it
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.junit.Test;
public class Test_Callable {
@Test
public void test() throws InterruptedException, ExecutionException {
System.out.println("Begin Test!");
int nThreadCnt=15;
List<Future<String>> result = new ArrayList<Future<String>>();
ExecutorService service = Executors.newCachedThreadPool();
for (int i=0;i<nThreadCnt;i++) {
result.add(service.submit(new CallableJob(i)));
}
//shutdown() will wait all the task to complete first
service.shutdown();
for (int i=0;i<nThreadCnt;i++) {
System.out.println("The result of task " + i + " is " + result.get(i).get());
}
System.out.println("End Test!");
}
}
class CallableJob implements Callable<String> {
public CallableJob(int id) {
this.id = id;
}
private int id ;
private int seed =Math.abs((new Random()).nextInt()) % 10;
@Override
public String call() throws Exception {
System.out.println("Begin job " + this.id);
Thread.yield();
for (int i=0;i<seed;i++) {
System.out.printf("Job id=%d, seed= %d of %d\n"
, this.id, i+1, this.seed);
Thread.yield();//give other thread opportunities to run
}
System.out.println("End job " + this.id);
return String.format("%s", this.seed);
}
}BTW, when using executor, and we wanna wait all the threads to end
, use awaitTermination() after shutdown()
//shutdown() will wait all the task to complete first
service.shutdown();
while(!service.awaitTermination(20, TimeUnit.MILLISECONDS)) { System.out.println("Waiting end......."); }
System.out.println("End Test!");
浙公网安备 33010602011771号