线程实例,线程池,重入锁,队列

  1. 线程池

public class TestCallableAndFuture {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        ExecutorService es = Executors.newFixedThreadPool(2);

        Callable<Integer> task1 = new Callable<Integer>(){
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 1; i <= 50; i++) {
                    sum += i;
                }
                return sum;
            }
        };

        Callable<Integer> task2 = new Callable<Integer>(){
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 51; i <= 100; i++) {
                    sum += i;
                }
                return sum;
            }
        };

        Future<Integer> f1 = es.submit(task1);

        Future<Integer> f2 = es.submit(task2);

        //等待中。。。造成当前线程阻塞(waiting)

        Integer result1 = f1.get();//以阻塞形式等待异步结果

        Integer result2 = f2.get();

        System.out.println(result1 + result2);


    }

}
  1. 重入锁,读写锁

public class TestReentrantReadWriteLock {

    public static void main(String[] args) {

        final MyEntity me = new MyEntity();

        Callable writeTask = new Callable(){
            @Override
            public Object call() throws Exception {
                me.setValue(111);
                return null;
            }
        };

        Callable readTask = new Callable(){
            @Override
            public Object call() throws Exception {
                me.getValue();
                return null;
            }
        };

        ExecutorService es = Executors.newFixedThreadPool(20);

        //开始
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < 2; i++) {
            es.submit(writeTask);
        }


        for (int i = 0; i < 18; i++) {
            es.submit(readTask);
        }

        es.shutdown();

        while(!es.isTerminated()){
            System.out.println("未完成...");
        }

        //结束
        long endTime = System.currentTimeMillis() - startTime;

        System.out.println(endTime);

    }

}


class MyEntity{

//  private ReentrantLock locker = new ReentrantLock();

    private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();//读写锁

    ReadLock rl = rwl.readLock();//获取读锁

    WriteLock wl = rwl.writeLock();//获取写锁


    private Integer value = 0;

    //写
    public void setValue(Integer value) throws InterruptedException{
        wl.lock();
        try {
            Thread.sleep(1000);
            this.value = value;
        } finally{
            wl.unlock();
        }
    }

    //读
    public Integer getValue() throws InterruptedException{
        rl.lock();
        try {
            Thread.sleep(1000);
            return this.value;
        } finally{
            rl.unlock();
        }
    }

}
  1. 队列, 解决生产者,消费者问题

public class TestArrayBlockingQueue {

    public static void main(String[] args) {

        final BlockingQueue<Character> bq = new LinkedBlockingQueue<Character>();

        Callable task1 = new Callable() {
            @Override
            public Object call() throws Exception {
                for (char c = 'A'; c <= 'Z'; c++) {
                    bq.put(c);
                    System.out.println("Put :" + c);
                }
                return null;
            }
        };

        Callable task2 = new Callable() {
            @Override
            public Object call() throws Exception {
                for (int i = 0; i < 26; i++) {
                    System.out.println("Take :" + bq.take());
                }
                return null;
            }
        };

        ExecutorService es = Executors.newFixedThreadPool(2);

        es.submit(task1);

        es.submit(task2);

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值