码迷,mamicode.com
首页 > 编程语言 > 详细

java线程池中以代码的顺序运行,主要是记录一下继承线程池的内容

时间:2018-11-28 15:38:11      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:syn   xtend   ack   block   over   pool   new   executors   java线程   

1.这个是自定义的线程池类,直接上代码

package org.jimmy.threadtest20181121;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutor201811281311 extends ThreadPoolExecutor {

    public static Runnable prevRunnable;
    
    public ThreadPoolExecutor201811281311(int corePoolSize,
            int maximumPoolSize, long keepAliveTime, TimeUnit unit,
            BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        if(prevRunnable != null){
            super.remove(prevRunnable);
        }
        super.beforeExecute(t, r);
    }

}

2.多线程操作的测试类,直接上代码

package org.jimmy.threadtest20181121;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestThread201811271616 {

    public static Semaphore semaphore = new Semaphore(1, true);
//    public static Semaphore semaphore = new Semaphore(1000, true);
    
    public static void main(String[] args) {
        try {
            long beginTime = new Date().getTime();
            TestThread201811271616 testThread201811271616 = new TestThread201811271616();
            LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
            ThreadPoolExecutor201811281311 executor = new ThreadPoolExecutor201811281311(1, 10000, 3600, TimeUnit.SECONDS, workQueue);
            /*ExecutorService executorService = Executors.newSingleThreadExecutor();
            for(int i = 0 ; i < 30; i++) {
                String id = i + "";
                Thread thread = new Thread(testThread201811271616.new LineUpThread(id));
                executorService.submit(thread);
            }
            executorService.shutdown();*/
            for(int i = 0 ; i < 1000; i++) {
                String id = i + "";
                Runnable runnable = testThread201811271616.new LineUpThread(id);
                Runnable prevRunnable = null;
                if(i > 0){
                    String prevId = (i - 1) + "";
                    prevRunnable = testThread201811271616.new LineUpThread(prevId);
                    ThreadPoolExecutor201811281311.prevRunnable = prevRunnable;
                }
                executor.execute(runnable);
            }
            executor.shutdown();
            while(!executor.isTerminated()){
                Thread.sleep(1);
            }
            long endTime = new Date().getTime();
            System.out.println("一共耗时:" + (endTime - beginTime) + "毫秒!");
            /*semaphore.acquireUninterruptibly();
            System.out.println("工作人员吃饭了,暂停服务!");
            semaphore.release();*/
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    class LineUpThread implements Runnable {

        private String id;
        
        public LineUpThread(String id) {
            this.id = id;
        }
        
        @Override
        public void run() {
            synchronized(this) {
                try {
                    semaphore.acquireUninterruptibly();
//                    System.out.println("id:" + id);
                    System.out.println("轮到编号" + id + "的客户了,可以开始购票了!");
                    System.out.println("编号" + id + "的客户已购票成功!");
                    semaphore.release();
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
    }

}

好了,我主要是记录给自己用的,有兴趣的自己看代码吧.

实际上,只要线程池只有一个容量,就一定是顺序执行的.与我代码中的删除无关.

java线程池中以代码的顺序运行,主要是记录一下继承线程池的内容

标签:syn   xtend   ack   block   over   pool   new   executors   java线程   

原文地址:https://www.cnblogs.com/JimmySeraph/p/10031776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!