分类:
2008-10-15 16:32:53
1. Exector 接口与runnable 接口的配合使用,引入线程池,不再使用start() 方法,节省了每次启动线程的资源开销
------------------------
import java.util.concurrent.*;
public class TestExecutor1 {
public static void main(String[] args) {
Runnable t1=new Target1();
Runnable t2=new Target2();
ExecutorService es=Executors.newFixedThreadPool(1);
es.execute(t1);
es.execute(t2);
}
}
class Target1 implements Runnable{
public void run(){
for(int i=1;i<=100;i++){
System.out.println(i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
}
}
class Target2 implements Runnable{
public void run(){
for(int i=1;i<=100;i++){
System.out.println(i+" $$$");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
}
}
2.Callable 接口代替runnable 接口,call()代替run() 方法,有返回值,可以抛异常
--------------------------
import java.util.concurrent.*;
public class TestExecutor2 {
public static void main(String[] args) throws Exception {
Callable c1=new MyCallable();
Callable c2=new MyCallable();
Callable c3=new MyCallable();
ExecutorService es=Executors.newFixedThreadPool(3);
Future f1=es.submit(c1);
Future f2=es.submit(c2);
Future f3=es.submit(c3);
System.out.println(f1.get());
System.out.println(f2.get());
System.out.println(f3.get());
es.shutdown();
}
}
class MyCallable implements Callable{
public String call() throws Exception{
int a=0;
int b=0;
for(int i=1;i<=1000;i++){
int r=(int)(Math.random()*1000)%2;
if (r==0) a++;
else b++;
}
if (a>b) return "Beijing";
else if (a
else throw new Exception("Equals");
}
}
3.lock ,condition 接口
-----------------------
import java.util.concurrent.locks.*;
public class TestSynchronized {
}
class MyStack{
char[] data=new char[6];
int index=0;
Lock lock=new ReentrantLock();
Condition full=lock.newCondition();
Condition empty=lock.newCondition();
public void push(char c) throws Exception{
try{
lock.lock();
while(data.length==index){
full.await();
}
data[index]=c;
index++;
empty.signalAll();
}
finally{
lock.unlock();
}
}
public char pop() throws Exception{
try{
lock.lock();
while(index==0){
empty.await();
}
index--;
char c=data[index];
data[index]=' ';
full.signalAll();
return c;
}
finally{
lock.unlock();
}
}
}
4.Semaphore 类在某些场合可以代替lock
--------------------
import java.util.*;
import java.util.concurrent.*;
public class TestSemaphore{
public static void main(String[] args) throws Exception{
BarberShop bs=new BarberShop();
ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(10);
Customer c;
for(int i=0;i<10;i++){
c=new Customer(String.valueOf(i+1),bs);
scheduler.schedule(c,(i+1)*2,TimeUnit.SECONDS);
}
}
}
class Barber{
private String name;
private boolean isBusy=false;
public Barber(String name){
this.name=name;
}
public void setBusy(boolean isBusy){
this.isBusy=isBusy;
}
public boolean isBusy(){
return this.isBusy;
}
public String getName(){
return this.name;
}
}
class BarberShop{
Collection barbers=new ArrayList();
int number=5;
Semaphore s=new Semaphore(5);
public BarberShop(){
Barber b=new Barber("Liucy");
barbers.add(b);
b=new Barber("Chenzq");
barbers.add(b);
b=new Barber("Liuxf");
barbers.add(b);
b=new Barber("Zhaodk");
barbers.add(b);
b=new Barber("Wanghg");
barbers.add(b);
}
public void haircut(Customer c) throws Exception{
//s.acquire();
boolean flag=s.tryAcquire();
if (!flag){
System.out.println(c.getName()+" waiting...");
s.acquire();
}
for(Barber b:barbers){
if (!b.isBusy()){
b.setBusy(true);
System.out.println(c.getName()+" Haircut by "+b.getName());
Thread.sleep(20000);
System.out.println(c.getName()+" exit,"+b.getName()+" is Free");
b.setBusy(false);
s.release();
return;
}
}
}
}
class Customer implements Runnable{
private String name;
private BarberShop shop;
public Customer(String name,BarberShop shop){
this.name=name;
this.shop=shop;
}
public String getName(){
return this.name;
}
public void run(){
[1]