多线程之 生产者消费者模型 线程池
发布日期:2022-04-11 08:52:44 浏览次数:10 分类:技术文章

本文共 5385 字,大约阅读时间需要 17 分钟。

多线程之三

  1. 使用synchronized 、wait() 、notifyAll()方法实现 生产者消费者模型
public class Demo01 {
private int GPU = 0; private static final int MAX_GPU = 10; private static final Boolean LOCK = true; class NVIDIA implements Runnable {
@Override public void run() {
while (true) {
synchronized (LOCK) {
if (GPU == MAX_GPU) {
try {
LOCK.wait(); } catch (InterruptedException e) {
e.printStackTrace(); } } GPU++; System.out.println(Thread.currentThread().getName() + "制造显卡后剩余" + GPU); LOCK.notifyAll(); } } } } class BTCBoss implements Runnable {
@Override public void run() {
while (true) {
synchronized (LOCK) {
if (GPU <= 0) {
try {
LOCK_BTC.wait(); } catch (InterruptedException e) {
e.printStackTrace(); } } GPU--; System.out.println(Thread.currentThread().getName() + "比特矿产老板抢购显卡后剩余" + GPU); LOCK.notifyAll(); } } } } public static void main(String[] args) {
Demo01 d = new Demo01(); NVIDIA nvidia = d.new NVIDIA(); BTCBoss btcBoss = d.new BTCBoss(); Thread t1 = new Thread(btcBoss); Thread t2 = new Thread(nvidia); t1.start(); t2.start(); }}
  1. 使用3种不同的方式创建线程
public class ThreadDemo01 {
//使用内部类实现Runnable public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+":"+i); } } }); Thread t2 = new Thread(new Runnable() {
@Override public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+":"+i); } } }); t1.start(); t2.start(); }}
public class ThreadDemo02 {
//继承Thread的方式 public static void main(String[] args) {
prints p1 =new prints(); prints p2 =new prints(); p1.start(); p2.start(); }}class prints extends Thread{
@Override public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+":"+i); } }}
import java.util.concurrent.Callable;import java.util.concurrent.FutureTask;public class ThreadDemo03 {
//实现Callable 接口的方式 public static void main(String[] args) {
Out o = new Out(); FutureTask
task = new FutureTask
(o); FutureTask
task2 = new FutureTask
(o); Thread t1 = new Thread(task); Thread t2 = new Thread(task2); t1.start(); t2.start(); }}class Out implements Callable
{
@Override public Integer call() throws Exception {
int sum = 0; for (int i = 0; i <= 100; i++) {
if(i%2==0){
sum+=i; } System.out.println(Thread.currentThread().getName()+"当前之和是:"+sum); } System.out.println(Thread.currentThread().getName()+"0到100的偶数之和是"+sum); return sum; }}
  1. 测试3种不同线程池,体会下区别
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ThreadPoolTest1 {
public static void main(String[] args) {
//此方式线程会一起执行 ExecutorService executorService = Executors.newCachedThreadPool(); Out o = new Out(); Out o2 = new Out(); executorService.submit(o); executorService.submit(o2); executorService.shutdown(); }}
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;//此方式线程最多有两个(我设置的) 在执行 有线程执行完毕出来 才执行其他线程public class ThreadPoolTest2 {
public static void main(String[] args) {
ExecutorService executorService2 = Executors.newFixedThreadPool(2); Out o1 = new Out(); Out o2 = new Out(); Out o3 = new Out(); executorService2.submit(o1); executorService2.submit(o2); executorService2.submit(o3); executorService2.shutdown(); }}
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;//此方式线程池里最多只有一个线程,在执行的时候没有第三者插足public class ThreadPoolTest3 {
public static void main(String[] args) {
ExecutorService executorService = Executors.newSingleThreadExecutor(); Out o1 =new Out(); Out o2 =new Out(); Out o3 =new Out(); executorService.submit(o1); executorService.submit(o2); executorService.submit(o3); executorService.shutdown(); }}
  1. 线程的线程的生命周期在不同状态做什么事情
不谈线程池 以Thread 创建的线程为例当用new 创建线程时 其线程并不执行 只有调用start方法时 才进入就绪状态 准备执行 当使用线程调度器调用此方法时 才开始执行当线程执行完毕 就会进入消亡状态 线程终止 当发生阻塞的情况会进入阻塞状态 解除后进入就绪状态等待线程调度器分配时间片

转载地址:https://blog.csdn.net/weixin_48322694/article/details/123884826 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:多线程之BackgroundWorker组件
下一篇:多线程中避免使用信号量

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月03日 09时06分55秒