1.用信号类semaphore
1.全部代码:
public class ThreadPoolTest {
//信号里
private static Semaphore semaphore=new Semaphore(5);//允许个数,相当于放了5把锁
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(new Runnable() {
public void run() {
method();
}
}).start();
}
}
private static void method() {
try {
semaphore.acquire();//发起信号
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadName="+Thread.currentThread().getName()+"我过来了");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadName="+Thread.currentThread().getName()+"出去了");
semaphore.release();//释放信号
}
}
2.步骤
1.创建信号类,传入允许线程数
private static Semaphore semaphore=new Semaphore(5);//允许个数,它表示一次性允许执行5线程
2.发起信号与结束信号:
semaphore.acquire();//发起信号
semaphore.release();//释放信号
在发起与释放信号中间,写入要执行的方法,
try {
semaphore.acquire();//发起信号
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadName="+Thread.currentThread().getName()+"我过来了");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadName="+Thread.currentThread().getName()+"出去了");
semaphore.release();//释放信号
2.用自带的线程池类固定线程池newFixedThreadPool
1.代码:
public class ThreadPoolTest2 {
private static Executor executors=Executors.newCachedThreadPool();//缓存线程池
private static Executor executors2=Executors.newFixedThreadPool(5);//固定线程池
private static Executor executors3=Executors.newScheduledThreadPool(5);//缓存线程池
private static Executor executors4=Executors.newSingleThreadExecutor();//单个线程池
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
executors2.execute(new Runnable() {
public void run() {
method();
}
});
}
}
private static void method() {
System.out.println("ThreadName="+Thread.currentThread().getName()+"开始执行");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadName="+Thread.currentThread().getName()+"执行结束");
}
}
2.步骤:
1.实例化固定线程池
private static Executor executors2=Executors.newFixedThreadPool(5);//固定线程池,表示一次只能执行5条子线程
2.执行
executors2.execute(new Runnable() {
public void run() {
method();
}
});
2.用自定义线程池–>线程池执行者:ThreadPoolExecutor
1.代码:
public class ThreadPoolTest3 {
public static void main(String[] args) {
LinkedBlockingQueue<Runnable> blockingQueue=new LinkedBlockingQueue(100);//100是该容器的最大上限
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 100, 1, TimeUnit.SECONDS, blockingQueue, new ThreadFactory() {
//线程安全的int的包装类,i++
AtomicInteger atomicInteger=new AtomicInteger(0);
public Thread newThread(Runnable r) {
//创建一个线程,然后把r复制给该线程
Thread thread = new Thread(r);
thread.setName("MyThread"+atomicInteger.getAndIncrement());
return thread;
}
});
for (int i = 0; i < 100; i++) {
threadPoolExecutor.execute(new Runnable() {
public void run() {
method();
}
});
}
}
private static void method() {
System.out.println("ThreadName="+Thread.currentThread().getName()+"我过来了");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadName="+Thread.currentThread().getName()+"出去了--------------------------");
}
}
2.步骤:
1.初始化线程池执行
使用的是五个参数的那个
参数介绍
- corePoolSize:表示核心线程池(一次性执行多少条线程,这里设置为5)
- maximumPoolSize:允许在线程池的最大线程池数量
- keepAliveTime:
- unit:
- workQueue:workQueue在执行任务之前用于保留任务的队列。该队列将仅保存由{@code execute}方法提交的{@code Runnable}任务。
示意图: