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

  1. 线程池

public class TestCallableAndFuture {

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

        ExecutorService es = Executors.newFixedThreadPool(2);

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

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

        Future f1 = es.submit(task1);

        Future 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 bq = new LinkedBlockingQueue();

        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);

    }

}

你可能感兴趣的:(CoreJava实例)