7:
public static void main(String[] args) throws InterruptedException {
Account acc = new Account();
acc.setBalance(1000);
int corePoolSize = 15;
int maximumPoolSize = 20;
long keepAliveTime = 2;
ThreadPoolExecutor excutor = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
excutor.prestartAllCoreThreads();
Lock lock = new ReentrantLock();
for (int i = 0; i < 10; i++) {
excutor.execute(new Runnable() {
@Override
public void run() {
lock.lock();
try {
if (acc.getBalance() >= 800) {
acc.setBalance(acc.getBalance()-800);
System.out.println("扣除800");
acc.setBalance(acc.getBalance()+800);
System.out.println("存入800");
}else{
System.out.println("警告:余额不足800");
}
}catch(Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
});
}
Thread.sleep(1000);
System.out.println("最终余额:"+ acc.getBalance());
excutor.shutdown();
}
8:
test:
public class Test83 {
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(50);
for (int i = 0; i < 100; i++) {
DBConnect dbThread = new DBConnect(semaphore);
try {
System.out.println(i+" 进入等待连接");
semaphore.acquire();
exec.execute(dbThread);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 退出线程池
exec.shutdown();
DB connect:
import java.util.Random;
import java.util.concurrent.Semaphore;
public class DBConnect extends Thread {
private Semaphore semaphore;
public DBConnect(Semaphore s) {
this.semaphore = s;
}
@Override
public void run() {
Random r=new java.util.Random();
int s = r.nextInt(5000)%(1001) +2000;
try {
System.out.println("Get DB Connection.");
Thread.sleep(s);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
semaphore.release();
// System.out.println(semaphore.availablePermits());//查询剩余资源
}
}
}
9:
public class Test {
public static void main(String[] args) throws IOException, InterruptedException {
CouponNumber cn = new CouponNumber();
cn.setNumber(100);
int corePoolSize = 200;
int maximumPoolSize = 500;
long keepAliveTime = 5;
ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
executor.prestartAllCoreThreads();
Lock lock = new ReentrantLock();
for(int i=0;i<300;i++) {
executor.execute(new Runnable() {
@Override
public void run() {
lock.lock();
try {
if (cn.getNumber() > 0) {
cn.setNumber(cn.getNumber()-1);
System.out.println("扣除1张");
}else{
System.out.println("警告:余额不足不能扣除");
}
System.out.println("当前余额"+cn.getNumber());
} catch (Exception e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
});
}
Thread.sleep(1000);
System.out.println("=======最终余额:"+cn.getNumber());
executor.shutdown();
}
}
Coupon num:
//import java.util.concurrent.locks.Lock;
//import java.util.concurrent.locks.ReentrantLock;
public class CouponNumber {
private int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
网友评论