线程池,Callable,ArrayBlockingQueue,MyArrayBlockingQueue通知机制

package com.peixinchen;

import java.util.concurrent.*;
import java.util.concurrent.ArrayBlockingQueue;

public class 线程池演示 {
static class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100000000; i++) {
System.out.println(“Hello”);
}
}
}

public static void main(String[] args) {
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            10,
            10,
            10,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(10),
            new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    return new Thread(r, "Hello");
                }
            }
    );
    ExecutorService executorService = threadPoolExecutor;
    Executor executor = threadPoolExecutor;

    executor.execute(new Runnable() {
        @Override
        public void run() {
            System.out.println("hello");
        }
    });

// executor.execute(new MyRunnable());
//
// for (int i = 0; i < 100000000; i++) {
// System.out.println(“World”);
// }

    executorService.shutdown();
}

}

package com.peixinchen;

import java.util.concurrent.*;
import java.util.concurrent.ArrayBlockingQueue;

public class 使用Callable {
static class MyCallable implements Callable {
private final int a;
private final int b;

    MyCallable(int a, int b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public Integer call() throws Exception {
        Thread.sleep(10_000);
        return a + b;
    }
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            10,
            10,
            10,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(10)
    );
    ExecutorService executorService = threadPoolExecutor;
    Executor executor = threadPoolExecutor;

    Future<Integer> future = executorService.submit(new MyCallable(10, 20));

    System.out.println("Hello");
    Integer r = future.get();
    System.out.println("World");
    System.out.println(r);

}

}

package com.peixinchen;

import java.util.concurrent.ArrayBlockingQueue;

public class QueueDemo {
static ArrayBlockingQueue queue = new ArrayBlockingQueue<>(10);

static class 生产者 extends Thread {
    @Override
    public void run() {
        setName("生产者");
        while (true) {
            try {
                queue.put(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

static class 消费者 extends Thread {
    @Override
    public void run() {
        setName("消费者");
        while (true) {
            try {
                queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public static void main(String[] args) {
    for (int i = 0; i < 1; i++) {
        Thread t = new 生产者();
        t.start();
    }

    for (int i = 0; i < 50; i++) {
        Thread t = new 消费者();
        t.start();
    }
}

}

package com.peixinchen;

// 线程安全问题 OK
// 通知机制
public class MyArrayBlockingQueue {
private final Integer[] array = new Integer[5];
private int size = 0;
private int headIndex = 0; // 队列中第一个元素所在下标
private int rearIndex = 0; // 队尾中,下一个可以放入元素的下标

// synchronized 修饰方法,视为对 this 加锁
public synchronized boolean offer(Integer e) throws InterruptedException {
    // 临界区开始
    while (size >= array.length) {
        wait();
    }

    // size 一定小于 array.length

    array[rearIndex] = e;
    rearIndex++;
    if (rearIndex == array.length) {
        rearIndex = 0;
    }
    size++;
    // 临界区结束

    notify();
    return true;
}

public synchronized Integer poll() throws InterruptedException {
    while (size <= 0) {
        wait();
    }
    // size 一定大于 0

    Integer e = array[headIndex];
    headIndex++;
    if (headIndex == array.length) {
        headIndex = 0;
    }
    size--;
    notify();       // 无法做精准唤醒

    return e;
}

}

package com.peixinchen;

// juc
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class Java中存在的几种阻塞队列 {
public static void main(String[] args) {
BlockingQueue queue = null;
}
}

package com.peixinchen;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class DBUtil {
// 单例模式
// volatile
private static volatile DataSource dataSource;

public static Connection getConnection() throws SQLException {
    if (dataSource == null) {
        synchronized (DBUtil.class) {
            if (dataSource == null) {
                MysqlDataSource mysqlDataSource = new MysqlDataSource();

                mysqlDataSource.setServerName("127.0.0.1");
                mysqlDataSource.setPort(3306);

                dataSource = mysqlDataSource;
            }
        }
    }

    return dataSource.getConnection();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值